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; eval eval( aString [, aBinding [ file [ line ] ] ]) -> anObject Evaluates the Ruby expression(s) in aString. If aBinding is given, the ...


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

Register FAQ Members List Calendar Mark Forums Read

Reply
 
Thread Tools Display Modes
  #11  
Old 04-03-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

eval
eval( aString [, aBinding [ file [ line ] ] ]) -> anObject


Evaluates the Ruby expression(s) in aString. If aBinding is given, the evaluation is performed in its context. The binding may be a Binding object or a Proc object. If the optional file and line parameters are present, they will be used when reporting syntax errors.

def getBinding(str)
return binding
end
str = "hello"
eval "str + ' Fred'" » "hello Fred"
eval "str + ' Fred'", getBinding("bye") » "bye Fred"

exec
exec( command [, args ])


Replaces the current process by running the given external command. If exec is given a single argument, that argument is taken as a line that is subject to shell expansion before being executed. If multiple arguments are given, the second and subsequent arguments are passed as parameters to command with no shell expansion. If the first argument is a two-element array, the first element is the command to be executed, and the second argument is used as the argv[0] value, which may show up in process listings. In MSDOS environments, the command is executed in a subshell; otherwise, one of the exec(2) system calls is used, so the running command may inherit some of the environment of the original program (including open file descriptors).

exec "echo *" # echoes list of files in current directory
# never get here


exec "echo", "*" # echoes an asterisk
# never get here
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12  
Old 04-03-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

exit
exit( anInteger=0 )


Initiates the termination of the Ruby script by raising the SystemExit exception. This exception may be caught. The optional parameter is used to return a status code to the invoking environment.

begin
exit
puts "never get here"
rescue SystemExit
puts "rescued a SystemExit exception"
end
puts "after begin block"

produces:

rescued a SystemExit exception
after begin block

Just prior to termination, Ruby executes any at_exit functions and runs any object finalizers (see ObjectSpace beginning on page 430).

at_exit { puts "at_exit function" }
ObjectSpace.define_finalizer(self, proc { puts "in finalizer" })
exit

produces:

at_exit function
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13  
Old 04-03-2008, 08:53 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

exit!
exit!( anInteger=-1 )


Similar to Kernel::exit , but exception handling, at_exit functions, and finalizers are bypassed.

fail

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


Synonym for Kernel::raise .

fork
fork [{ block } ] -> aFixnum or nil
Creates a subshell. If a block is specified, that block is run in the subshell, and the subshell terminates with a status of zero. Otherwise, the fork call returns twice, once in the parent, returning the process id of the child, and once in the child, returning nil. The child process can exit using Kernel::exit! to avoid running any at_exit functions. The parent process should use Process::wait to collect the termination statuses of its children; otherwise, the operating system may accumulate zombie processes.

fork do
3.times {|i| puts "Child: #{i}" }
end
3.times {|i| puts "Parent: #{i}" }
Process.wait

produces:

Parent: 0
Child: 0
Parent: 1
Child: 1
Parent: 2
Child: 2
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14  
Old 04-06-2008, 08:35 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

format
format( aString [, anObject ]* ) -> aString


Synonym for Kernel::sprintf .

gets
gets( aString=$/ ) -> aString or nil


Returns (and assigns to $_) the next line from the list of files in ARGV (or $*), or from standard input if no files are present on the command line. Returns nil at end of file. The optional argument specifies the record separator. The separator is included with the contents of each record. A separator of nil reads the entire contents, and a zero-length separator reads the input one paragraph at a time, where paragraphs are divided by two consecutive newlines. If multiple filenames are present in ARGV, gets(nil) will read the contents one file at a time.

ARGV << "testfile"
print while gets

produces:

This is line one
This is line two
This is line three
And so on...
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15  
Old 04-06-2008, 08:37 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

global_variables
global_variables -> anArray


Returns an array of the names of global variables.

global_variables.grep /std/ » ["$stdin", "$stderr", "$stdout"]

gsub
gsub( pattern, replacement ) -> aString
gsub( pattern ) {| | block }

-> aString


Equivalent to $_.gsub..., except that $_ receives the modified result.

$_ = "quick brown fox"
gsub /[aeiou]/, '*' »"q**ck br*wn f*x"
$_ »"q**ck br*wn f*x"
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16  
Old 04-06-2008, 08:48 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

load
load( aFileName, wrap=false ) -> true


Loads and executes the Ruby program in the file aFileName. If the filename does not resolve to an absolute path, the file is searched for in the library directories listed in $:. If the optional wrap parameter is true, the loaded script will be executed under an anonymous module, protecting the calling program's global namespace. Any local variables in the loaded file will not be propagated to the loading environment.

gsub!

gsub!( pattern, replacement ) -> aString or nil
gsub!( pattern ) {| | block }

-> aString or nil


Equivalent to Kernel::gsub , except nil is returned if $_ is not modified.

$_ = "quick brown fox"
gsub! /cat/, '*' » nil
$_ » "quick brown fox"
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17  
Old 04-06-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

local_variables
local_variables -> anArray


Returns the names of the current local variables.

fred = 1
for i in 1..10
# ...
end
local_variables »["fred", "i"]

loop
loop {| | block }
Repeatedly executes the block.

loop {
print "Input: "
break if !gets or $_ =~ /^qQ/
# ...
}
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18  
Old 04-07-2008, 08:04 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

open
open( aString [, aMode [ perm ] ] ) -> anIO or nil
open( aString [, aMode [ perm ] ] ) {| anIO | block }

-> nil


Creates an IO object connected to the given stream, file, or subprocess.

If aString does not start with a pipe character (``|''), treat it as the name of a file to open using the specified mode defaulting to ``r'' (see the table of valid modes on page 326). If a file is being created, its initial permissions may be set using the integer third parameter.

If a block is specified, it will be invoked with the File object as a parameter, and the file will be automatically closed when the block terminates. The call always returns nil in this case.

If aString starts with a pipe character, a subprocess is created, connected to the caller by a pair of pipes. The returned IO object may be used to write to the standard input and read from the standard output of this subprocess. If the command following the ``|'' is a single minus sign, Ruby forks, and this subprocess is connected to the parent. In the subprocess, the open call returns nil. If the command is not ``-'', the subprocess runs the command. If a block is associated with an open("|-") call, that block will be run twice---once in the parent and once in the child. The block parameter will be an IO object in the parent and nil in the child. The parent's IO object will be connected to the child's $stdin and $stdout. The subprocess will be terminated at the end of the block.

open("testfile") do |f|
print f.gets
end

produces:

This is line one

Open a subprocess and read its output:

cmd = open("|date")
print cmd.gets
cmd.close

produces:

Sun Jun 9 00:19:39 CDT 2002

Open a subprocess running the same Ruby program:


f = open("|-", "w+")
if f == nil
puts "in Child"
exit
else
puts "Got: #{f.gets}"
end

produces:

Got: in Child

Open a subprocess using a block to receive the I/O object:

open("|-") do |f|
if f == nil
puts "in Child"
else
puts "Got: #{f.gets}"
end
end

produces:


Got: in Child
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19  
Old 04-07-2008, 08:05 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

p
p( [ anObject ]+ ) -> nil


For each object, directly writes anObject.inspect followed by the current output record separator to the program's standard output. p bypasses the Ruby I/O libraries.

p self

produces:

main
__________________
Shaalini.S
Be the Best of Whatever you are...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #20  
Old 04-07-2008, 08:06 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

print
print( [ anObject ]* ) -> nil


Prints each object in turn to $defout. If the output field separator ($,) is not nil, its contents will appear between each field. If the output record separator ($\) is not nil, it will be appended to the output. If no arguments are given, prints $_. Objects that aren't strings will be converted by calling their to_s method.

print "cat", [1,2,3], 99, "\n"
$, = ", "
$\ = "\n"
print "cat", [1,2,3], 99

produces:


cat12399
cat, 1, 2, 3, 99

printf

printf( anIO, aString [, anObject ]* ) -> nil
printf( aString [, anObject ]* ) -> nil
Equivalent to:anIO. write sprintf( aString, anObject ...)
or
$defout.write sprintf( aString, anObject ...)
__________________
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 02:50 PM.


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