Hi All, working through understanding loops. Would someone explain why
I cannot use “next if i=3…6” or “next if i=(3…6)” in code below.
After each I inserted what I thought I should see as a result versus
what I did get returned. - Scott
i=0
loop do
i+=1
next if i==3
print "#{i} "
break if i==10
end
expected => 1 2 4 5 6 7 8 9 10
actual => 1 2 4 5 6 7 8 9 10
(yeah!)
i=0
loop do
i+=1
next if i== 3…6
print "#{i} "
break if i==10
end
expected => 1 2 7 8 9 10
actual => warning: integer literal in conditional range
(don’t understand, thought it was telling me it does not work because it
can’t transform the range on the fly to the actual integers 3,4,5,6)
i=0
loop do
i+=1
next if i== (3…6)
print "#{i} "
break if i==10
end
expected => 1 2 7 8 9 10
actual => 1 2 3 4 5 6 7 8 9 10
(so I thought by wrapping the range in a “()” it would convert to
3,4,5,6 and then be able to jump over these integers on output)
Am 11.06.2013 18:04, schrieb Scott H.:
actual => 1 2 3 4 5 6 7 8 9 10
(so I thought by wrapping the range in a “()” it would convert to
3,4,5,6 and then be able to jump over these integers on output)
This might help:
2.0.0-p195 :018 > (3…6).include? 5
=> true
2.0.0-p195 :019 > (3…6).include? 10
=> false
Note that a Range and an Integer can never be equal.
Am 11.06.2013 18:27, schrieb [email protected]:
This might help:
2.0.0-p195 :018 > (3…6).include? 5
=> true
2.0.0-p195 :019 > (3…6).include? 10
=> false
PS. In fact, it would also be possible to use the === operator
(which is also used in case statements), like this:
2.0.0-p195 :020 > (3…6) === 5
=> true
But I wouldn’t advise to do that. The range needs to be on
the left side, or it won’t work. (Which might lead to bugs
that would be difficult to locate.)
Joel P. wrote in post #1112054:
These work:
(3…6).include? 4
4.between? 3, 6
How would I incorporate the above into the loop? I tried…
next if i.between? 3, 6
result => undefined method ‘between’ for 1:Fixnum
unknown wrote in post #1112056:
Am 11.06.2013 18:04, schrieb Scott H.:
actual => 1 2 3 4 5 6 7 8 9 10
(so I thought by wrapping the range in a “()” it would convert to
3,4,5,6 and then be able to jump over these integers on output)
This might help:
2.0.0-p195 :018 > (3…6).include? 5
=> true
2.0.0-p195 :019 > (3…6).include? 10
=> false
Note that a Range and an Integer can never be equal.
You state a range can never be equal to an integer. Does that mean
i ==
so is that why (3…6) is not working for me?
Would it make any difference that I’m using 1.9.3-p392?
Did i mention I’m new to ruby and programming
3…6 is a Range, and Integers like 1 are class Fixnum. You can see more
of what Ranges are capable of here:
You’d get “undefined method ‘between’” if you forgot the question mark
from “between?”. It’s general practice in Ruby for methods which return
a Boolean to end with a question mark.
Scott H. [email protected] wrote:
(don’t understand, thought it was telling me it does not work because it
can’t transform the range on the fly to the actual integers 3,4,5,6)
Clippy sez: “It appears you are trying to use the flip-flop operator.”
Flip-flop is a bit odd, but this should work:
i=0
loop do
i+=1
next if i==3 … i==6 # note the range is the comparisons
print "#{i} "
break if i==10
end
output:
1 2 7 8 9 10
a (possibly) more idiomatic way to write that would be:
1.upto(10) do |i|
next if i==3 … i==6
print "#{i} "
end
Still another, perhaps more useful:
1.upto(10).select{|i| true unless i==3 … i==6 }
which just returns the collection, suitable for other uses besides
printing.
Or, just use the #include? on the subranges as others have
suggested.
On Tue, Jun 11, 2013 at 11:10 PM, Scott H. [email protected] wrote:
=> true
2.0.0-p195 :019 > (3…6).include? 10
=> false
Note there is also the === operator:
irb(main):002:0> (3…6) === 5
=> true
irb(main):003:0> (3…6) === 10
=> false
Note that a Range and an Integer can never be equal.
You state a range can never be equal to an integer. Does that mean
i ==
so is that why (3…6) is not working for me?
It’s a general rule that for equivalence the class needs to be
identical.
I have blogged about this here:
http://blog.rubybestpractices.com/posts/rklemme/018-Complete_Class.html
Search for “Equivalence” in the page.
Would it make any difference that I’m using 1.9.3-p392?
No.
Did i mention I’m new to ruby and programming
Kind regards
robert
Am 11.06.2013 23:12, schrieb Scott H.:
Joel P. wrote in post #1112054:
These work:
(3…6).include? 4
4.between? 3, 6
How would I incorporate the above into the loop? I tried…
next if i.between? 3, 6
result => undefined method ‘between’ for 1:Fixnum
That’s not possible, the error message does not match your input
(you must have used next if i.between 3, 6
).
Helpful! Appreciate the additional examples tamouse.
Robert, thanks for the link to equivalence.