Sup guys :wave:, more of a ruby question. Is there...
# general
d
Sup guys 👋, more of a ruby question. Is there a way to do an early print and call
next
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
s
puts
returns
nil
which is considered false-y so the "right-hand side" of your
and
is not evaluated...
🙌 2
To get your example working you would have to spread it over multiple lines:
Copy code
(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:
Copy code
# 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 }
d
Awesome @Severin; thanks so much!. Given that I think
p
instead of
puts
will work.