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

G

Gunnar

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
 
J

Jacob Fugal

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-talk/91137
 
G

Gunnar

Jacob said:
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-talk/91137
OK.
Thank you for answering so very quickly
Gunnar
 
R

Robert Klemme

Gunnar said:
Jacob said:
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-talk/91137
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.
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:"+itemoutside:1
outside:2
inside:3
inside:4
inside:5
outside:6
outside:6
=> "1\n2\n3\n4\n5\n6\n6\n"
Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top