R
Ruby Freak
In the code below, a block, { x += 1 } is magically passed to
thrice ?? or something like that.
I don't understand the usage of the "thrice { iterator }" using {}
and why I can't make a normal block syntax work. The thrice method
doesn't take (normal) parameters, so how does this work?
Ruby, Have you been messing around behind my back?
Thanks in advance.
def thrice
yield
yield
yield
end
x = 5
puts "value of x before: #{x}" # => 5
thrice { x += 1 }
puts "value of x after: #{x}" # => 8
# broken from here down
thrice { |x| x += 1 }
thrice do |x|
x += 1
end
thrice ?? or something like that.
I don't understand the usage of the "thrice { iterator }" using {}
and why I can't make a normal block syntax work. The thrice method
doesn't take (normal) parameters, so how does this work?
Ruby, Have you been messing around behind my back?
Thanks in advance.
def thrice
yield
yield
yield
end
x = 5
puts "value of x before: #{x}" # => 5
thrice { x += 1 }
puts "value of x after: #{x}" # => 8
# broken from here down
thrice { |x| x += 1 }
thrice do |x|
x += 1
end