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; proc proc { block } -> aProc Creates a new procedure object from the given block. Equivalent to Proc.new . aProc = proc { &...


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

Register FAQ Members List Calendar Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
  #21 (permalink)  
Old 04-07-2008, 08:07 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

proc
proc { block } -> aProc


Creates a new procedure object from the given block. Equivalent to Proc.new .

aProc = proc { "hello" }
aProc.call»"hello"

putc

putc( anInteger ) -> anInteger

Equivalent to $defout.putc( anInteger ).

puts
puts( [ args ]* ) -> nil
Equivalent to $defout.puts( args )
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #22 (permalink)  
Old 04-08-2008, 08:07 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

raise
raise( aString )
raise( anException [, aString [ anArray ] ] )


With no arguments, raises the exception in $! or raises a RuntimeError if $! is nil. With a single String argument, raises a RuntimeError with the string as a message. Otherwise, the first parameter should be the name of an Exception class (or an object that returns an Exception when sent exception). The optional second parameter sets the message associated with the exception, and the third parameter is an array of callback information. Exceptions are caught by the rescue clause of begin...end blocks.

raise "Failed to create socket"
raise ArgumentError, "No parameters", caller
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #23 (permalink)  
Old 04-08-2008, 08:09 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

rand
rand( max=0 ) -> aNumber
Converts max to an integer using max1 = max.to_i.abs. If the result is zero, returns a pseudorandom floating point number greater than or equal to 0.0 and less than 1.0. Otherwise, returns a pseudorandom integer greater than or equal to zero and less than max1. Kernel::srand may be used to ensure repeatable sequences of random numbers between different runs of the program.

srand 1234 » 0
[ rand, rand ] » [0.7408769294, 0.2145348572]
[ rand(10), rand(1000) ] » [3, 323]
srand 1234 » 1234
[ rand, rand ] » [0.7408769294, 0.2145348572]


readline
readline( [ aString=$/ ] ) -> aString

Equivalent to Kernel::gets , except readline raises EOFError at end of file.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #24 (permalink)  
Old 04-08-2008, 08:11 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

readlines
readlines( [ aString=$/ ] ) -> anArray


Returns an array containing the lines returned by calling Kernel.gets(aString) until the end of file.

require
require( aString ) -> true or false
Ruby tries to load the library named aString, returning true if successful. If the filename does not resolve to an absolute path, it will be searched for in the directories listed in $:. If the file has the extension ``.rb'', it is loaded as a source file; if the extension is ``.so'', ``.o'', or ``.dll'',[Or whatever the default shared library extension is on the current platform.] Ruby loads the shared library as a Ruby extension. Otherwise, Ruby tries adding ``.rb'', ``.so'', and so on to the name. The name of the loaded feature is added to the array in $". A feature will not be loaded if it already appears in $". require returns true if the feature was successfully loaded.

require "my-library.rb"
require "db-driver"
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #25 (permalink)  
Old 04-08-2008, 08:12 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

scan
scan( pattern ) -> anArray
scan( pattern ) {| | block } -> $_


Equivalent to calling $_.scan. See String#scan on page 373.

select
select( readArray [, writeArray [ errorArray [ timeout ] ] ] ) -> anArray or nil


Performs a low-level select call, which waits for data to become available from input/output devices. The first three parameters are arrays of IO objects or nil. The last is a timeout in seconds, which should be an Integer or a Float. The call waits for data to become available for any of the IO objects in readArray, for buffers to have cleared sufficiently to enable writing to any of the devices in writeArray, or for an error to occur on the devices in errorArray. If one or more of these conditions are met, the call returns a three-element array containing arrays of the IO objects that were ready. Otherwise, if there is no change in status for timeout seconds, the call returns nil. If all parameters are nil, the current thread sleeps forever.

select( [$stdin], nil, nil, 1.5 ) » [[#<IO:0x401ba090>], [], []]
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #26 (permalink)  
Old 04-08-2008, 08:15 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

set_trace_func
set_trace_func( aProc ) -> aProc
set_trace_func( nil ) -> nil
Establishes aProc as the handler for tracing, or disables tracing if the parameter is nil. aProc takes up to six parameters: an event name, a filename, a line number, an object id, a binding, and the name of a class. aProc is invoked whenever an event occurs. Events are: c-call (call a C-language routine), c-return (return from a C-language routine), call (call a Ruby method), class (start a class or module definition), end (finish a class or module definition), line (execute code on a new line), raise (raise an exception), and return (return from a Ruby method). Tracing is disabled within the context of aProc
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #27 (permalink)  
Old 04-09-2008, 08:09 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

singleton_method_added
singleton_method_added( aFixnum ) -> nil


Invoked with a symbol id whenever a singleton method is added to a module or a class. The default implementation in Kernel ignores this, but subclasses may override the method to provide specialized functionality.

class Test
def Test.singleton_method_added(id)
puts "Added #{id.id2name} to Test"
end
def a() end
def Test.b() end
end
def Test.c() end

produces:

Added singleton_method_added to Test
Added b to Test
Added c to Test
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #28 (permalink)  
Old 04-09-2008, 08:10 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

sleep
sleep( [ aNumeric ] ) -> aFixnum


Suspends the current thread for aNumber seconds (which may be a Float with fractional seconds). Returns the actual number of seconds slept (rounded), which may be less than that asked for if the thread was interrupted by a SIGALRM, or if another thread calls Thread#run . An argument of zero causes sleep to sleep forever.

Time.new » Sun Jun 09 00:19:40 CDT 2002
sleep 1.2 » 1
Time.new » Sun Jun 09 00:19:41 CDT 2002
sleep 1.9 » 2
Time.new » Sun Jun 09 00:19:43 CDT 2002
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #29 (permalink)  
Old 04-09-2008, 08:12 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

sprintf
sprintf( aFormatString [, arguments ]* ) -> aString


Returns the string resulting from applying aFormatString to any additional arguments. Within the format string, any characters other than format sequences are copied to the result. A format sequence consists of a percent sign, followed by optional flags, width, and precision indicators, then terminated with a field type character. The field type controls how the corresponding sprintf argument is to be interpreted, while the flags modify that interpretation.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #30 (permalink)  
Old 04-09-2008, 08:15 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

srand
srand( [ aNumber ] ) -> oldSeed


Seeds the pseudorandom number generator to the value of aNumber.to_i.abs. If aNumber is omitted or zero, seeds the generator using a combination of the time, the process id, and a sequence number. (This is also the behavior if Kernel::rand is called without previously calling srand, but without the sequence.) By setting the seed to a known value, scripts can be made deterministic during testing. The previous seed value is returned.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #31 (permalink)  
Old 04-09-2008, 08:17 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Red face Re: Ruby BuiltIn functions

sub
sub( pattern, replacement ) -> $_
sub( pattern ) { block } -> $_


Equivalent to $_.sub(args), except that $_ will be updated if substitution occurs.

sub!
sub!( pattern, replacement ) -> $_ or nil
sub!( pattern ) { block } -> $_ or nil
Equivalent to $_.sub!(args).
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #32 (permalink)  
Old 04-09-2008, 08:25 PM
KiruthikaSambandam KiruthikaSambandam is offline
D-Web Analyst
 
Join Date: Aug 2007
Posts: 332
KiruthikaSambandam is on a distinguished road
Default Re: Ruby BuiltIn functions

proc
lambda

Returns newly created procedure object from the block. The procedure object is the instance of the class Proc.

putc(c)


Writes the character c to the default output ($>).

putc(obj..)

Writes an obj to the default output ($>), then newline for each arguments.
raise([error_type,][message][,traceback])
fail([error_type,][message][,traceback])

Raises an exception. In no argument given, re-raises last exception. With one arguments, raises the exception if the argument is the exception. If the argument is the string, raise creates a new RuntimeError exception, and raises it. If two arguments supplied, raise creates a new exception of type error_type, and raises it.

If the optional third argument traceback is specified, it must be the traceback infomation for the raising exception in the format given by variable $@ or caller function.

The exception is assigned to the variable $!, and the position in the source file is assigned to the $@.

If the first argument is not an exception class or object, the exception actually raised is determined by calling it's exception method (baring the case when the argument is a string in the second form). The exception method of that class or object must return it's representation as an exception.

The fail is an alias of the raise.

Thanks
Kiruthika
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #33 (permalink)  
Old 04-09-2008, 08:26 PM
KiruthikaSambandam KiruthikaSambandam is offline
D-Web Analyst
 
Join Date: Aug 2007
Posts: 332
KiruthikaSambandam is on a distinguished road
Default Re: Ruby BuiltIn functions

rand(max)

Returns a random integer number greater than or equal to 0 and less than the value of max. (max should be positive.) Automatically calls srand unless srand() has already been called.

If max is 0, rand returns a random float number greater than or equal to 0 and less than 1.

readlines([rs])

Reads entire lines from the virtual concatenation of each file listed on the command line or standard input (in case no files specified), and returns an array containing the lines read.

Lines are separated by the value of the optional argument rs, which default value is defined by the variable $/.

require(feature)

Demands a library file specified by the feature. The feature is a string to specify the module to load. If the extension in the feature is ".so", then Ruby interpreter tries to load dynamic-load file. If the extension is ".rb", then Ruby script will be loaded. If no extension present, the interpreter searches for dynamic-load modules first, then tries to Ruby script. On some system actual dynamic-load modules have extension name ".o", ".dll" or something, though require always uses the extension ".so" as a dynamic-load modules.

require returns true if modules actually loaded. Loaded module names are appended in $".

Thanks
Kiruthika
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #34 (permalink)  
Old 04-09-2008, 08:27 PM
KiruthikaSambandam KiruthikaSambandam is offline
D-Web Analyst
 
Join Date: Aug 2007
Posts: 332
KiruthikaSambandam is on a distinguished road
Default Re: Ruby BuiltIn functions

select(reads[, writes[, excepts[, timeout]]])

Calls select(2) system call. Reads, writes, excepts are specified arrays containing instances of the IO class (or its subclass), or nil.

The timeout must be either an integer, Float, Time, or nil. If the timeout is nil, select would not time out.

select returns nil in case of timeout, otherwise returns an array of 3 elements, which are subset of argument arrays.

sleep([sec])

Causes the script to sleep for sec seconds, or forever if no argument given. May be interrupted by sending the process a SIGALRM or run from other threads (if thread available). Returns the number of seconds actually slept. sec may be a floating-point number.

split([sep[, limit]])

Return an array containing the fields of the string, using the string sep as a separator. The maximum number of the fields can be specified by limit.

Thanks
Kiruthika
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #35 (permalink)  
Old 04-15-2008, 08:29 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Wink Re: Ruby BuiltIn functions

throw
throw( aSymbol [, anObject ] )


Transfers control to the end of the active catch block waiting for aSymbol. Raises NameError if there is no catch block for the symbol. The optional second parameter supplies a return value for the catch block, which otherwise defaults to nil. For examples, see Kernel::catch on page 413.

trace_var

trace_var( aSymbol, aCmd ) -> nil
trace_var( aSymbol ) {| val | block }-> nil
Controls tracing of assignments to global variables. The parameter aSymbol identifies the variable (as either a string name or a symbol identifier). cmd (which may be a string or a Proc object) or block is executed whenever the variable is assigned. The block or Proc object receives the variable's new value as a parameter. Also see Kernel::untrace_var .

trace_var :$_, proc {|v| puts "$_ is now '#{v}'" }
$_ = "hello"
$_ = ' there'

produces:


$_ is now 'hello'
$_ is now ' there'
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #36 (permalink)  
Old 04-15-2008, 08:30 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

trap
trap( signal, cmd ) -> anObject
trap( signal ) {| | block } -> anObject


Specifies the handling of signals. The first parameter is a signal name (a string such as ``SIGALRM'', ``SIGUSR1'', and so on) or a signal number. The characters ``SIG'' may be omitted from the signal name. The command or block specifies code to be run when the signal is raised. If the command is the string ``IGNORE'' or ``SIG_IGN'', the signal will be ignored. If the command is ``DEFAULT'' or ``SIG_DFL'', the operating system's default handler will be invoked. If the command is ``EXIT'', the script will be terminated by the signal. Otherwise, the given command or block will be run.

The special signal name ``EXIT'' or signal number zero will be invoked just prior to program termination.

trap returns the previous handler for the given signal.

trap 0, proc { puts "Terminating: #{$$}" }
trap("CLD") { puts "Child died" }
fork && Process.wait

produces:


Terminating: 1425
Child died
Terminating: 1424
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #37 (permalink)  
Old 04-15-2008, 08:33 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

untrace_var
untrace_var( aSymbol [, aCmd ] ) -> anArray or nil
Removes tracing for the specified command on the given global variable and returns nil. If no command is specified, removes all tracing for that variable and returns an array containing the commands actually removed.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #38 (permalink)  
Old 04-15-2008, 08:42 PM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

dump
dump( anObject [, anIO ] , limit=--1 ) -> anIO


Serializes anObject and all descendent objects. If anIO is specified, the serialized data will be written to it, otherwise the data will be returned as a String. If limit is specified, the traversal of subobjects will be limited to that depth. If limit is negative, no checking of depth will be performed.

class Klass
def initialize(str)
@str = str
end
def sayHello
@str
end
end

o = Klass.new("hello\n")
data = Marshal.dump(o)
obj = Marshal.load(data)
obj.sayHello » "hello\n"
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #39 (permalink)  
Old 04-17-2008, 01:48 AM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Default Re: Ruby BuiltIn functions

load
load( from [, aProc ] ) -> anObject
Returns the result of converting the serialized data in from into a Ruby object (possibly with associated subordinate objects). from may be either an instance of IO or an object that responds to to_str. If proc is specified, it will be passed each object as it is deserialized.
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #40 (permalink)  
Old 04-17-2008, 01:50 AM
shaalini shaalini is offline
D-Web Analyst
 
Join Date: Apr 2007
Posts: 342
shaalini is on a distinguished road
Exclamation Re: Ruby BuiltIn functions

atan2
Math.atan2( y, x ) -> aFloat
Computes the arc tangent given y and x. Returns -PI..PI.

cos

Math.cos( aNumeric ) -> aFloat
Computes the cosine of aNumeric (expressed in radians). Returns -1..1.

exp

Math.exp( aNumeric ) -> aFloat
Returns e raised to the power of aNumeric.

frexp
Math.frexp( aNumeric ) -> anArray
Returns a two-element array ([aFloat, aFixnum]) containing the normalized fraction and exponent of aNumeric.
__________________
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 On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is Ruby for Web? S.Vinothkumar Ruby 6 11-20-2007 01:25 AM
What are Virtual Functions? How to implement virtual functions in "C"? Sabari C and C++ Programming 4 09-10-2007 10:35 PM
What Is RUBY? theseokit Ruby 7 03-12-2007 11:43 PM
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:51 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.