[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

and statement

loveajax

3/4/2008 8:18:00 AM

Hi,

I found the following statement in routing.rb of rails framework. I
haven't seen this kind of usage anywhere. Can somebody tell me what
does this statement do? I am particularly interested in knowing how
'and' behaves here.

result = route.recognize(path, environment) and return result

Thanks
-subbu
10 Answers

Robert Klemme

3/4/2008 9:05:00 AM

0

2008/3/4, loveajax@gmail.com <loveajax@gmail.com>:
> Hi,
>
> I found the following statement in routing.rb of rails framework. I
> haven't seen this kind of usage anywhere. Can somebody tell me what
> does this statement do? I am particularly interested in knowing how
> 'and' behaves here.
>
> result = route.recognize(path, environment) and return result

If /result/ is not /nil/ and not /false/ it will be returned here.
Otherwise control flow proceeds to the next line. Try it out in IRB

irb(main):001:0> def t(x) x and return x; 'not_returned' end
=> nil
irb(main):002:0> t 1
=> 1
irb(main):003:0> t 2
=> 2
irb(main):004:0> t false
=> "not_returned"
irb(main):005:0> t nil
=> "not_returned"
irb(main):006:0> t true
=> true
irb(main):007:0>

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

7stud --

3/4/2008 9:11:00 AM

0

unknown wrote:
> Hi,
>
> I found the following statement in routing.rb of rails framework. I
> haven't seen this kind of usage anywhere. Can somebody tell me what
> does this statement do? I am particularly interested in knowing how
> 'and' behaves here.
>
> result = route.recognize(path, environment) and return result
>

result = true and true
puts result

result = false and false
puts result

puts

x = 10
result = false and x = 20
puts result
puts x

result = true and x = 20
puts result
puts x

--output:--
true
false

false
10
true
20


The results are due to 'short circuiting' of the conditionals. If you
have this statement:

x and y

and x is false, then there is no way for the whole conditional to
evaluate to true. As a result, there is no need to evaluate the second
expression y to determine the result of the conditional--its going to be
false no matter what y evaluates to, and ruby chooses not to evaluate y.

The statement:

result = route.recognize(path, environment) and return result

is equivalent to:

result = route.recognize(path, environment)
if result
return result
end
--
Posted via http://www.ruby-....

Subbu

3/4/2008 10:25:00 AM

0

Thank you so much. I looked up 'and' && operands in the PickAxe book
and this is what it says:
"The 'and' and && operators evaluate their first operand. If false,
the expression returns the value of the first operand; otherwise, the
expression returns the value of the second operand"

I am really loving my Ruby journey.
-subbu

7stud --

3/4/2008 1:22:00 PM

0

Subbu wrote:
> Thank you so much. I looked up 'and' && operands in the PickAxe book
> and this is what it says:
> "The 'and' and && operators evaluate their first operand. If false,
> the expression returns the value of the first operand; otherwise, the
> expression returns the value of the second operand"
>

Which doesn't appear to be true. Look at this:

result = (x=20)
puts result #20


result = true and x=20
puts result #true


The output isn't the same.
--
Posted via http://www.ruby-....

Lionel Bouton

3/4/2008 2:24:00 PM

0

7stud -- wrote:
> Subbu wrote:
>> Thank you so much. I looked up 'and' && operands in the PickAxe book
>> and this is what it says:
>> "The 'and' and && operators evaluate their first operand. If false,
>> the expression returns the value of the first operand; otherwise, the
>> expression returns the value of the second operand"
>>
>
> Which doesn't appear to be true. Look at this:
>
> result = (x=20)
> puts result #20
>
>
> result = true and x=20
> puts result #true

Precedence...

result = (true and x = 20)

7stud --

3/4/2008 3:13:00 PM

0

Lionel Bouton wrote:
> 7stud -- wrote:
>> result = (x=20)
>> puts result #20
>>
>>
>> result = true and x=20
>> puts result #true
>
> Precedence...
>
> result = (true and x = 20)

Ah. Thanks.

--
Posted via http://www.ruby-....

yermej

3/4/2008 9:18:00 PM

0

On Mar 4, 8:23 am, Lionel Bouton <lionel-subscript...@bouton.name>
wrote:
> 7stud -- wrote:
> > Subbu wrote:
> >> Thank you so much. I looked up 'and' && operands in the PickAxe book
> >> and this is what it says:
> >> "The 'and' and && operators evaluate their first operand. If false,
> >> the expression returns the value of the first operand; otherwise, the
> >> expression returns the value of the second operand"
>
> > Which doesn't appear to be true. Look at this:
>
> > result = (x=20)
> > puts result #20
>
> > result = true and x=20
> > puts result #true
>
> Precedence...
>
> result = (true and x = 20)

Or:

result = true && x = 20

The precedence is the only difference between 'and' and '&&'.

Subbu

3/10/2008 4:28:00 PM

0

On Mar 4, 1:18 pm, yermej <yer...@gmail.com> wrote:
> On Mar 4, 8:23 am, Lionel Bouton <lionel-subscript...@bouton.name>
> wrote:
>
>
>
> > 7stud -- wrote:
> > > Subbu wrote:
> > >> Thank you so much. I looked up 'and' && operands in the PickAxe book
> > >> and this is what it says:
> > >> "The 'and' and && operators evaluate their first operand. If false,
> > >> the expression returns the value of the first operand; otherwise, the
> > >> expression returns the value of the second operand"
>
> > > Which doesn't appear to be true. Look at this:
>
> > > result = (x=20)
> > > puts result #20
>
> > > result = true and x=20
> > > puts result #true
>
> > Precedence...
>
> > result = (true and x = 20)
>
> Or:
>
> result = true && x = 20
>
> The precedence is the only difference between 'and' and '&&'.

Does that mean = has higher precedence than 'and'?

Justin Collins

3/10/2008 11:40:00 PM

0

Subbu wrote:
> On Mar 4, 1:18 pm, yermej <yer...@gmail.com> wrote:
>
>> On Mar 4, 8:23 am, Lionel Bouton <lionel-subscript...@bouton.name>
>> wrote:
>>
>>
>>
>>
>>> 7stud -- wrote:
>>>
>>>> Subbu wrote:
>>>>
>>>>> Thank you so much. I looked up 'and' && operands in the PickAxe book
>>>>> and this is what it says:
>>>>> "The 'and' and && operators evaluate their first operand. If false,
>>>>> the expression returns the value of the first operand; otherwise, the
>>>>> expression returns the value of the second operand"
>>>>>
>>>> Which doesn't appear to be true. Look at this:
>>>>
>>>> result = (x=20)
>>>> puts result #20
>>>>
>>>> result = true and x=20
>>>> puts result #true
>>>>
>>> Precedence...
>>>
>>> result = (true and x = 20)
>>>
>> Or:
>>
>> result = true && x = 20
>>
>> The precedence is the only difference between 'and' and '&&'.
>>
>
> Does that mean = has higher precedence than 'and'?
>
>

Yes.

http://phrogz.net/ProgrammingRuby/language.html#...


Subbu

3/11/2008 5:30:00 AM

0

On Mar 10, 4:40 pm, Justin Collins <justincoll...@ucla.edu> wrote:
> Subbu wrote:
> > On Mar 4, 1:18 pm, yermej <yer...@gmail.com> wrote:
>
> >> On Mar 4, 8:23 am, Lionel Bouton <lionel-subscript...@bouton.name>
> >> wrote:
>
> >>> 7stud -- wrote:
>
> >>>> Subbu wrote:
>
> >>>>> Thank you so much. I looked up 'and' && operands in the PickAxe book
> >>>>> and this is what it says:
> >>>>> "The 'and' and && operators evaluate their first operand. If false,
> >>>>> the expression returns the value of the first operand; otherwise, the
> >>>>> expression returns the value of the second operand"
>
> >>>> Which doesn't appear to be true. Look at this:
>
> >>>> result = (x=20)
> >>>> puts result #20
>
> >>>> result = true and x=20
> >>>> puts result #true
>
> >>> Precedence...
>
> >>> result = (true and x = 20)
>
> >> Or:
>
> >> result = true && x = 20
>
> >> The precedence is the only difference between 'and' and '&&'.
>
> > Does that mean = has higher precedence than 'and'?
>
> Yes.
>
> http://phrogz.net/ProgrammingRuby/language.html#...

Great. Thanks.