Statements and Expressions

 
   

Ruby Way
By Hal Fulton
Slots : 1.0
Table of Contents
 


There is little distinction between a statement and an expression; both return values. A statement can be made into an expression by enclosing it in parentheses, which will allow it to be used as an argument.

 

 arr = [1,2, 3 if limit>2] # parse error arr = [1,2, (3 if limit>2)] # OK f(limit or 1)    # parse error f( (limit or 1) )   # OK 

In this case, the parentheses for the method are required to distinguish it from the parentheses that make the statement into an expression acceptable as an argument.

Ruby has || and &&, in addition to or and and. These are similar in meaning, but form an expression instead of a statement because they bind more tightly.

Another effect of statements having a value is that using return is optional in method definitions. The value of the last statement in the definition will be used as the return value in such a case.

In Ruby, you will use eval for evaluating both expressions and statements. Use an optional binding or proc where you would otherwise have used an optional global or local dict in Python as a namespace. There is no equivalent to Python's exec function. The exec method in Ruby is used to execute a shell command. Ruby's backtick operator is another form of expressing such shell commands.


   

 

 



The Ruby Way
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2000
Pages: 119
Authors: Hal Fulton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net