IT Community - Software Programming, Web Development and Technical Support

Ruby BuiltIn functions

This is a discussion on Ruby BuiltIn functions within the Ruby forums, part of the Web Development category; Hi, Can u explain about ruby built-in functions?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Ruby

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 03-30-2008, 08:27 PM
bluesky bluesky is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 667
bluesky is on a distinguished road
Lightbulb Ruby BuiltIn functions

Hi,
Can u explain about ruby built-in functions?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 03-30-2008, 08:28 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

A function is "a portion of code within a larger program, performs a specific task".

Functions have many benefits including:

1. reducing the duplication of code in a program (e.g., by replicating useful functionality, such as mathematical functions);
2. enabling reuse of code across multiple programs;
3. decomposing complex problems into simpler pieces (this improves maintainability and ease of extension);
4. improving readability of a program;
5. hiding or regulating part of the program.

Functions are also know under many other names such as subroutines, methods or subprograms

abort
Array
at_exit {...}
binding
block_given?
callcc {| c|...}
caller([ n])
catch( tag) {...}
chomp([ rs=$/])
chomp!([ rs=$/])
chop
chop!
eval( str[, scope[, file, line]])
exec( cmd[, arg...])
exit([ result=0])
exit!([ result=0])
fail(...)
Float( obj)
fork
fork {...}
format( fmt[, arg...])
gets([ rs=$/])
global_variables
gsub( x, y)
gsub( x) {...}
gsub!( x, y)
gsub!( x) {...}
Integer( obj)
lambda {| x|...}
proc {| x|...}
lambda
proc
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 03-30-2008, 08:30 PM
satheesh satheesh is offline
D-Web Programmer
 
Join Date: Aug 2007
Posts: 96
satheesh is on a distinguished road
Default Re: Ruby BuiltIn functions

Hi,
Here are some functions which i know
oad( file[, private=false])
local_variables
loop {...}
open( path[, mode="r"])
open( path[, mode="r"]) {| f|...}
p( obj) print([ arg...])
printf( fmt[, arg...])
proc {| x|...}
putc( c)
puts([ str])
raise(...)
fail(...)
srand([ seed])
String( obj)
syscall( sys[, arg...])
system( cmd[, arg...])
sub( x, y)
sub( x) {...}
trap( sig, cmd)
trap( sig) {...}
untrace_var( var[, cmd]
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 03-30-2008, 08:40 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

abort
Terminate execution immediately, effectively by calling Kernel.exit(1).

Array
Array( arg ) -> anArray
Returns arg .to_a.
Array(1..5) » [1, 2, 3, 4, 5]


at_exit

at_exit { block } -> aProc
Converts block to a Proc object (and therefore binds it at the point of call) and registers it for execution when the program exits. If multiple handlers are registered, they are executed in reverse order of registration.

def do_at_exit(str1)
at_exit { print str1 }
end
at_exit { puts "cruel world" }
do_at_exit("goodbye ")
exit

produces:


goodbye cruel world


binding

binding -> aBinding

Returns a Binding object, describing the variable and method bindings at the point of call. This object can be used when calling eval to execute the evaluated command in this environment. Also see the description of Binding beginning on page 291.

def getBinding(param)
return binding
end
b = getBinding("hello")
eval "param", b » "hello"
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 03-31-2008, 08:49 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

block_given?

block_given? -> true or false


Returns true if yield would execute a block in the current context.

def try
if block_given?
yield
else
"no block"
end
end
try » "no block"
try { "hello" } » "hello"
try do
"hello"
end
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 03-31-2008, 08:50 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

callcc
callcc {| cont | block } -> anObject


Generates a Continuation object, which it passes to the associated block. Performing a cont .call will cause the callcc to return (as will falling through the end of the block). The value returned by the callcc is the value of the block, or the value passed to cont .call. See Continuation on page 294 for more details. Also see Kernel::throw for an alternative mechanism for unwinding a call stack.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 03-31-2008, 08:51 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

caller
caller( [ anInteger ] ) -> anArray


Returns the current execution stack---an array containing strings in the form ``file:line'' or ``file:line: in `method'''. The optional anInteger parameter determines the number of initial stack entries to omit from the result.

def a(skip)
caller(skip)
end
def b(skip)
a(skip)
end
def c(skip)
b(skip)
end
c(0) » ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10"]
c(1) » ["prog:5:in `b'", "prog:8:in `c'", "prog:11"]
c(2) » ["prog:8:in `c'", "prog:12"]
c(3) » ["prog:13"]
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 03-31-2008, 08:52 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

catch
catch( symbol ) {| | block }-> anObject


catch executes its block. If a throw is executed, Ruby searches up its stack for a catch block with a tag corresponding to the throw's symbol. If found, that block is terminated, and catch returns the value given to throw. If throw is not called, the block terminates normally, and the value of catch is the value of the last expression evaluated. catch expressions may be nested, and the throw call need not be in lexical scope.

def routine(n)
puts n
throw :done if n <= 0
routine(n-1)
end


catch(:done) { routine(3) }

produces:

3
2
1
0
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old 03-31-2008, 08:55 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

chomp
chomp( [ aString ] ) -> $_ or aString
Equivalent to $_ = $_.chomp(aString). See String#chomp on page 367.

$_ = "now\n"
chomp » "now"
$_»"now"
chomp "ow"»"n"
$_ »"n"
chomp "xxx"»"n"
$_»"n"

chomp!
chomp!( [ aString ] ) -> $_ or nil


Equivalent to $_.chomp!(aString). See String#chomp!

$_ = "now\n"
chomp! » "now"
$_»"now"
chomp! "x"»nil
$_ » "now"
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10  
Old 04-03-2008, 08:49 PM
shaalini shaalini is offline
D-Web Architect
 
Join Date: Apr 2007
Posts: 633
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

chop
chop -> aString
Equivalent to ($_.dup).chop!, except nil is never returned. See String#chop!

a = "now\r\n"
$_ = a
chop » "now"
$_ » "now"
chop » "no"
chop » "n"
chop » ""
chop » ""
a » "now\r\n"

chop!
chop! -> $_ or nil


Equivalent to $_.chop!.

a = "now\r\n"
$_ = a
chop! » "now"
chop! » "no"
chop! » "n"
chop! » ""
chop! » nil
$_ » ""
a » ""
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
What Is RUBY? theseokit Ruby 12 10-21-2008 11:07 PM
PHP vs Ruby suman PHP Programming 2 10-02-2008 12:59 AM
Is Ruby for Web? S.Vinothkumar Ruby 6 11-20-2007 01:25 AM
Ruby within .NET? econwriter5 Ruby 0 03-07-2007 05:02 PM
Ruby IDE drecko Ruby 0 02-16-2007 12:11 AM


All times are GMT -7. The time now is 09:40 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0