Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Invalid retry and compile error (SyntaxError)!

I have 2 error messages, Invalid retry compile error (SyntaxError)

I heard that retry does not exist in Ruby 1.9 and I exactly am using Ruby 1.9.3.3p392 does any can help?

11 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey all, we're going to update these videos. They were filmed on an older version of Ruby where this syntax works. We'll be updating them shortly. In the mean time, you can skip the retry part. Sorry for the trouble!

Hoormazd Kia
Hoormazd Kia
Courses Plus Student 6,075 Points

Has this problem still not been fixed? this was 7 months ago?

I think the syntax for retry changed from ruby -v 1.9.

To use retry, it needs to be in a rescue block inside a begin block.

http://blog.mirthlab.com/2012/05/25/cleanly-retrying-blocks-of-code-after-an-exception-in-ruby/

&

http://blog.mirthlab.com/2012/05/25/cleanly-retrying-blocks-of-code-after-an-exception-in-ruby/

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey all, just a quick note that we're redoing the Ruby curriculum and fixing this!

same issue here (v 2.0.0 / Win 8). Code:

animals = %w(dog cat horse parrot goat shrimp snail cow)
count = 0

for animal in animals
    puts "The current animal is #{animal}"
    break if count == 10
    count += 1
    retry if animal == 'horse'
end
Antoine Boillot
Antoine Boillot
10,466 Points

Hi all,

Fyi, I found that statement...

"Inside a rescue block is the only valid location for retry, all other uses will raise a SyntaxError."

...Here.

Looks like the Begin/Rescue block is the only way to use Retry now.

I tried the following code and get the expected result :

animals = %w(dog cat horse goat snake frog)
count = 0

begin
    animals.each do |animal|
        puts "The current animal is #{animal}"
        break if count == 10
        count += 1
        raise if animal == 'horse'
    end
rescue
    retry
end

The following could have worked as well :

animals = %w(dog cat horse goat snake frog)
count = 0

begin
    for animal in animals
        puts "The current animal is #{animal}"
        break if count == 10
        count += 1
        raise if animal == 'horse'
    end
rescue
    retry
end

Hope this helps.

PS : I'm using Ruby 2.1.3

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hi Dongju Seo can you give us any more information about what you're working on? Perhaps paste some code here or a link to the code challenge?

Everything works just fine as of now, but I assume this problem was caused when I used 'rvm' to specifically pick up 'ruby 1.9.3'.. I am perfectly following Treebook tutorials.

I came across this same error when running loops-11.rb in the Control Flow lesson in Loops for Ruby Foundations. I'm using ruby 2.0.0p247

Chriss-MacBook-Pro-2:loops chrismoore$ ruby loops-11.rb loops-11.rb:9: Invalid retry loops-11.rb: compile error (SyntaxError)

here is my code:

animals = %w(dog cat horse goat snake frog)

count = 0

for animal in animals puts "The current animal is #{animal}" break if count == 10 count += 1 retry if animal == 'horse' end

I am also having the same problem using Ruby 2.0.0 p247

animals = %w(dog cat horse goat snake frog)
count = 0

for animal in animals
  puts "The current animal is #{animal}"
  count +=1
  break if count ==10
  retry if animal == "horse
end

When I replace retry with redo, the program works.

I am also having the same problem using Ruby 2.0.0 p247

animals = %w(dog cat horse goat snake frog) count = 0

for animal in animals puts "The current animal is #{animal}" count +=1 break if count ==10 retry if animal == "horse" end When I replace retry with redo, the program works.

Having the same issue.

I am having the same issue..my input is nearly identical to Mikal Chawdry's version above. There are slight differences- and I do not have enough experience to recognize whether or not these differences matter- so I have pasted my input below. Also, I am using ruby 1.9.3p448 and textmate v2.0. Here are the contents of loops-11.rb (my textmate file that I am running on ruby):

animals = %w(dog cat horse goat snake frog)
count = 0

for animal in animals
  puts "The current animal is #{animal}"
  break if count == 10
  count += 1
  retry if animal == 'horse'
end

When I run ruby loops-11.rb in the terminal, I receive the following:

loops-11.rb:8: Invalid retry
loops-11.rb: compile error (SyntaxError)

As noted in previous answers, the redo command works as expected. When I edit loops-11.rbso that retry is replaced with redo, I get:

The current animal is dog
The current animal is cat
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse
The current animal is horse

However, since redo and retry aren't interchangeable, this is not a solution. Guess I'll move on to the next video for now.

bump, same here.

The user "xclite" on dreamincode.net posted this response which I found helpful:

That answers the question. Retry was changed in Ruby 1.9 to only work on a rescue clause - you can only use it to "retry" a begin end block that has failed. For example:

x = 1 begin puts "hello" raise "CRAP" if x == 1 rescue Exception x = 2 retry end

Same stuff here, redo works, retry don't!

Cristian Daniel Marquez Barrios
Cristian Daniel Marquez Barrios
8,900 Points

U update the video but didnt delete the other, still the bug!!!

Fernando Gomez
Fernando Gomez
18,527 Points

Still no update!!! same error:

Invalid retry
compile error(SyntaxError)