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?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| Hi, Can u explain about ruby built-in functions? |
|
#2
| |||
| |||
| 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... |
|
#3
| |||
| |||
| 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] |
|
#4
| |||
| |||
| 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... |
|
#5
| |||
| |||
| 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... |
|
#6
| |||
| |||
| 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... |
|
#7
| |||
| |||
| 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... |
|
#8
| |||
| |||
| 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... |
|
#9
| |||
| |||
| 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... |
|
#10
| |||
| |||
| 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... |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |
Our Partners |