Syntax error

ERROR

Test6.rb:4 syntax error, unexpected tSYMBEG, expecting kDO or ‘<’ or ‘<’
e.times do+y:x|

test6.rb:5: syntax error, unexpected tSTRING_BEG, expecting kDO or ‘<’
or ‘<’
puts “Looping #{y}”

test6.rb:7: syntax error, unexpected kEND, expecting $end]

ERROR

well here is the code

CODE

x = 1200
puts “Before the loop, x = #{x}”

3.times do|y:x|
puts “Looping #{y}”
x = y
end

puts “After the loop, x = #{x}”

CODE

What are you expecting the “x:y” statement to do? I ask because I’m not
familiar with that syntax, and “:” doesn’t appear alone on any list of
operators I’ve seen. Otherwise I only know it as the second operator of
the ternary operator ?:

In any case, what again are you trying to accomplish? There’s probably
a better way that doesn’t have a syntax error.

Alex S. wrote:

What are you expecting the “x:y” statement to do? I ask because I’m not
familiar with that syntax, and “:” doesn’t appear alone on any list of
operators I’ve seen. Otherwise I only know it as the second operator of
the ternary operator ?:

In any case, what again are you trying to accomplish? There’s probably
a better way that doesn’t have a syntax error.

Sorry I forgot to post this.

I was doing that above the summary in the code, above the output.

John S. wrote:

Sorry I forgot to post this.
Learn How to Blog and Build Websites for Profit!
I was doing that above the summary in the code, above the output.

If I read the example correctly, the colon (:slight_smile: should be a semi-colon
(;).

js

I have done that and it says:
Test6.rb:4 syntax error, unexpected ‘;’, tCOLON2 or ‘[’ or ‘.’
3.times do |y;X|

test6.rb:5: syntax error, unexpected tSTRING_BEG, expecting kDO or ‘<’
or ‘<’
puts “Looping #{y}”

test6.rb:7: syntax error, unexpected kEND, expecting $end]

Anthony Ob wrote:

As the page notes, this was introduced in Ruby 1.9.1. I’m guessing you
are using an older version.

-Justin

Sorry I forgot to post this.
http://rubylearning.com/satishtalim/ruby_blocks.html
I was doing that above the summary in the code, above the output.

If I read the example correctly, the colon (:slight_smile: should be a semi-colon
(;).

js

Justin C. wrote:

Anthony Ob wrote:

As the page notes, this was introduced in Ruby 1.9.1. I’m guessing you
are using an older version.

-Justin

I was, then I downloaded and installed version 1.9.2

Got it - in your code, you typed “:”, while the example uses “;”.

And to expand a bit on what I typed below, the “:” is used to indicate
symbols, like “:x”. So when you had the typo “|y:x|”, ruby thinks
there’s two items there - “y” and “:x”.

Anthony Ob wrote:

Justin C. wrote:

Anthony Ob wrote:

As the page notes, this was introduced in Ruby 1.9.1. I’m guessing you
are using an older version.

-Justin

I was, then I downloaded and installed version 1.9.2

Yes, I saw the same error in 1.8.6 but not in 1.9.2.

js

Alex S. wrote:

Got it - in your code, you typed “:”, while the example uses “;”.

And to expand a bit on what I typed below, the “:” is used to indicate
symbols, like “:x”. So when you had the typo “|y:x|”, ruby thinks
there’s two items there - “y” and “:x”.

I realised that mistake of using : instead of ;
But I am still getting an error

Test6.rb:4 syntax error, unexpected ‘;’, tCOLON2 or ‘[’ or ‘.’
3.times do |y;X|

test6.rb:5: syntax error, unexpected tSTRING_BEG, expecting kDO or ‘<’
or ‘<’
puts “Looping #{y}”

test6.rb:7: syntax error, unexpected kEND, expecting $end

Works fine on 1.8.7.

Test6.rb:4 syntax error, unexpected ‘;’, tCOLON2 or ‘[’ or ‘.’
3.times do |y;X|

test6.rb:5: syntax error, unexpected tSTRING_BEG, expecting kDO or ‘<’
or ‘<’
puts “Looping #{y}”

test6.rb:7: syntax error, unexpected kEND, expecting $end

One other note: you have an uppercase X instead of a lower case x.

js

Alex S. wrote:

Works fine on 1.8.7.

I tried 1.8.6, and 1.9.2
and here is what I got:
http://img684.imageshack.us/img684/6434/errorwy.png

John S. wrote:

Test6.rb:4 syntax error, unexpected ‘;’, tCOLON2 or ‘[’ or ‘.’
3.times do |y;X|

test6.rb:5: syntax error, unexpected tSTRING_BEG, expecting kDO or ‘<’
or ‘<’
puts “Looping #{y}”

test6.rb:7: syntax error, unexpected kEND, expecting $end

One other note: you have an uppercase X instead of a lower case x.

js

I looked at the code there was no uppercase x, I am baffled.

I think that you are still using 1.8.6 when you get this. Look at the
value of RUBY_VERSION. I see the exact error message in 1.8.6.

js

John S. wrote:

I think that you are still using 1.8.6 when you get this. Look at the
value of RUBY_VERSION. I see the exact error message in 1.8.6.

js

Sorry, my fault it was still, 1.8.6. My apologizes.

Anthony Ob wrote:

John S. wrote:

I think that you are still using 1.8.6 when you get this. Look at the
value of RUBY_VERSION. I see the exact error message in 1.8.6.

js

Sorry, my fault it was still, 1.8.6. My apologizes.

No problem. Have fun.

js