Skip to content
Tags

,

Of ‘and’ and ‘&&’ and ‘or’ and ‘||’

June 22, 2011
 I

ruby-1.9.2-p180 :032 > a = 4
 => 4
ruby-1.9.2-p180 :033 > b = nil
 => nil
ruby-1.9.2-p180 :034 > c = a and b
 => nil
ruby-1.9.2-p180 :035 > c
 => 4
ruby-1.9.2-p180 :036 > c = a && b
 => nil
ruby-1.9.2-p180 :037 > c
 => nil
ruby-1.9.2-p180 :038 > a = nil
 => nil
ruby-1.9.2-p180 :039 > b = 4
 => 4
ruby-1.9.2-p180 :040 > c = a and b
 => nil
ruby-1.9.2-p180 :041 > c
 => nil
ruby-1.9.2-p180 :042 > c = a && b
 => nil
ruby-1.9.2-p180 :043 > c
 => nil
ruby-1.9.2-p180 :044 > a = 4
 => 4
ruby-1.9.2-p180 :045 > b = nil
 => nil
ruby-1.9.2-p180 :046 > c = a or b
 => 4
ruby-1.9.2-p180 :047 > c
 => 4
ruby-1.9.2-p180 :048 > c = a || b
 => 4
ruby-1.9.2-p180 :049 > c
 => 4
ruby-1.9.2-p180 :050 > a = nil
 => nil
ruby-1.9.2-p180 :051 > b = 4
 => 4
ruby-1.9.2-p180 :052 > c = a or b
 => 4
ruby-1.9.2-p180 :053 > c
 => nil
ruby-1.9.2-p180 :054 > c = a || b
 => 4
ruby-1.9.2-p180 :055 > c
 => 4

n short the rules are this: 
when using either ‘and’ or ‘or’ c necessarily gets the value of the first variabe
when using ‘&&’ or ‘||’ c necessarily gets the value returned by the expression
Note all and, &&, or and || use short circuit evaluation

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: