Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Error in Ruby, Unexpected Pipe Character in a Do

Tags:

ruby

I'll try to be concise this time around! I'm still working Project Euler, this time back to #2. My real issue here is I'm terrible with Ruby. When I run the following code

x = 1
y = 2
sum = 2
while x >= 4_000_000 do |x|

  sum += y if y % 2 == 0

  z = x + y

  x = x ^ y   # xor magic
  y = x ^ y   # xor magic
  x = x ^ y   # xor magic

  y = z 
end

p sum

My interpreter kicks out the following output:

/Users/Andy/Documents/Programming/Ruby/ProjectEuler/P2.rb:4: syntax error, unexpected '|'
while x >= 4_000_000 do |x|
                         ^

I'm reading why's (Poignant) Guide to Ruby, and I'm pretty sure I have the pipe syntax correct for the Do. Could someone point out what I'm doing wrong here? I've tried messing around in a lot of different ways and am coming up short handed

like image 574
Oso Avatar asked Jun 23 '26 09:06

Oso


1 Answers

while (x >= 4_000_000)
    foo
end

You don't even have to pass in x, because it's accessible in the scope of the enclosing block.

like image 169
Borealid Avatar answered Jun 26 '26 03:06

Borealid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!