[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Pickaxe page 342, Ranges in Boolean Expressions --I don't understand

Gunnar

5/3/2005 4:40:00 PM

Hi all
I can not make sense of the boolean ranges. The example
(11..20).collect {|i| (i%4==0.. i%3==0)?i:nil} => [nil, 12, nil, nil,
nil, 16, 17, 18, nil, 20].

I can figure out all numbers except the '18', which in my opinion should be
a 'nil' since i%3==0 is true and would cause the state to change from SET
to UNSET. Is the state machine Fig 22.1 correct?
Gunnar
3 Answers

Jacob Fugal

5/3/2005 5:03:00 PM

0

On 5/3/05, Gunnar <gunnar.larsson2@comhem.se> wrote:
> Hi all
> I can not make sense of the boolean ranges. The example
> (11..20).collect {|i| (i%4==0.. i%3==0)?i:nil} => [nil, 12, nil, nil,
> nil, 16, 17, 18, nil, 20].
>
> I can figure out all numbers except the '18', which in my opinion should be
> a 'nil' since i%3==0 is true and would cause the state to change from SET
> to UNSET. Is the state machine Fig 22.1 correct?

It appears to me that the second condition in the range UNSETs, but
inclusively (which makes sense for the semantics of '..'). That's why,
for example, 12 is printed at all... 12%4==0, so it SETs, retval is
set to true since it is SET, then 12%3==0 so it UNSETs. But the retval
is already set, and it returns true, thus printing 12.

In the same way, when we get to 18, the state machine is already SET
(since 16). retval is set to true since it is SET, then 18%3==0 so it
unsets. But just like with 12, retval is already set, so it returns
true and prints 18.

For more information see [ruby-talk:91137].

Jacob Fugal

[ruby-talk:91137] http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-...



Gunnar

5/3/2005 5:24:00 PM

0

Jacob Fugal wrote:

> On 5/3/05, Gunnar <gunnar.larsson2@comhem.se> wrote:
>> Hi all
>> I can not make sense of the boolean ranges. The example
>> (11..20).collect {|i| (i%4==0.. i%3==0)?i:nil} => [nil, 12, nil, nil,
>> nil, 16, 17, 18, nil, 20].
>>
>> I can figure out all numbers except the '18', which in my opinion should
>> be a 'nil' since i%3==0 is true and would cause the state to change from
>> SET to UNSET. Is the state machine Fig 22.1 correct?
>
> It appears to me that the second condition in the range UNSETs, but
> inclusively (which makes sense for the semantics of '..'). That's why,
> for example, 12 is printed at all... 12%4==0, so it SETs, retval is
> set to true since it is SET, then 12%3==0 so it UNSETs. But the retval
> is already set, and it returns true, thus printing 12.
>
> In the same way, when we get to 18, the state machine is already SET
> (since 16). retval is set to true since it is SET, then 18%3==0 so it
> unsets. But just like with 12, retval is already set, so it returns
> true and prints 18.
>
> For more information see [ruby-talk:91137].
>
> Jacob Fugal
>
> [ruby-talk:91137]
> [http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-...
OK.
Thank you for answering so very quickly
Gunnar

Robert Klemme

5/3/2005 5:45:00 PM

0

Gunnar wrote:
> Jacob Fugal wrote:
>
>> On 5/3/05, Gunnar <gunnar.larsson2@comhem.se> wrote:
>>> Hi all
>>> I can not make sense of the boolean ranges. The example
>>> (11..20).collect {|i| (i%4==0.. i%3==0)?i:nil} => [nil, 12, nil,
>>> nil, nil, 16, 17, 18, nil, 20].
>>>
>>> I can figure out all numbers except the '18', which in my opinion
>>> should be a 'nil' since i%3==0 is true and would cause the state to
>>> change from SET to UNSET. Is the state machine Fig 22.1 correct?
>>
>> It appears to me that the second condition in the range UNSETs, but
>> inclusively (which makes sense for the semantics of '..'). That's
>> why, for example, 12 is printed at all... 12%4==0, so it SETs,
>> retval is set to true since it is SET, then 12%3==0 so it UNSETs.
>> But the retval is already set, and it returns true, thus printing 12.
>>
>> In the same way, when we get to 18, the state machine is already SET
>> (since 16). retval is set to true since it is SET, then 18%3==0 so it
>> unsets. But just like with 12, retval is already set, so it returns
>> true and prints 18.
>>
>> For more information see [ruby-talk:91137].
>>
>> Jacob Fugal
>>
>> [ruby-talk:91137]
>> [http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-...
> OK.
> Thank you for answering so very quickly
> Gunnar

It's historic: Perl does it the same way. It's useful because you can
easily match ranges (for example of line comments, that start with
"=begin" and end with "=end" line) etc.

>> s=<<EOS
1
2
3
4
5
6
6
EOS
=> "1\n2\n3\n4\n5\n6\n6\n"
>>
?> s.each do |item|
?> puts /3/ =~ item .. /5/ =~ item ? "inside:"+item : "outside:"+item
>> end
outside:1
outside:2
inside:3
inside:4
inside:5
outside:6
outside:6
=> "1\n2\n3\n4\n5\n6\n6\n"
>>

Kind regards

robert