I’m trying out to have a block in a while and begin statements in Ruby, but I get a syntax error. Any other way to implement it?
Here’s what I want to accomplish
(1..limit).each { |i|
while (true) do |n|
x = n * (i%n)
puts n if n%i != 0
break if x.even? && !x.zero?
n += 1
end
}
whileis a keyword, so you do not need the block. Your code should be:But you are requesting a block variable form the
whilestatement. Variable names inside the pipes are for variables passed to your block containing information from whatever calls your block. I will assume thatnis supposed to increment. Here is a working version:If you really need the code in a block, you could create one and then call it, like this (ruby 1.9 only):
By the way, here is a cleaner version: