Diego Michel
12/21/2022, 4:54 AMnext
in a single line. I tried doing something like this and it doesn't work as I expected, I was expecting it to print "ONE" and then "2":
3.1.1 :005 > (1..5).each do |n| puts "ONE" and next if n==1 ; puts n end
ONE
1
2
3
4
5
=> 1..5
Severin
12/21/2022, 8:04 AMputs
returns nil
which is considered false-y so the "right-hand side" of your and
is not evaluated...Severin
12/21/2022, 8:07 AM(1..5).each do |n|
if n == 1
puts 'ONE'
next
end
puts n
end
Or use some other for m for the conditional check:
# Ternary operator
(1..5).each { |n| puts(n == 1 ? 'ONE' : n) }
# Or if-then-else clause
(1..5).each { |n| if n == 1 then puts 'ONE' else puts n end }
Diego Michel
12/21/2022, 8:21 AMp
instead of puts
will work.