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 trialEivind Jonassen
5,994 PointsGetting an error when practicing "retry" command in for-loops.
Hi! Getting the following errors when trying to run my code: "Invalid retry" "Compile error (SyntaxError) Here is the 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
4 Answers
Chris Shaw
26,676 PointsHi Eivind,
I literally just finished this course tonight but I did a little research and was able to gather that triggering an raise
exception on if animal == 'horse'
within a begin...end
block works for Ruby 1.9+.
animals = %w(dog cat horse goat snake frog)
count = 0
animals.each do |animal|
begin
puts "The current animal is #{animal}"
break if count == 10
count += 1
raise if animal == 'horse'
rescue Exception
retry
end
end
This is not an ideal solution but for the purpose of this example it works, I tested this in workspaces and it gave me the desired result, happy coding =)
/cc Jason Seifer
Eivind Jonassen
5,994 PointsThis is no longer valid syntax it turns out.. An earlier post about the same issue has Jason Seifer commenting this:
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!
This was 10 months ago. Nor sure what shortly means in Treehouse's vocabulary.. :p
Ricardo Garcia
5,939 Pointsim still waiting for the updated videos :S
Matthew Pierce
15,779 Points16 months and still waiting!
Antoine Boillot
10,466 PointsHi Chris,
I tried your code but cannot get the same result as Jason in the video.
Here is my code (looks like it is the same as you did) :
animals = %w(dog cat horse goat snake frog)
count = 0
animals.each do |animal|
begin
puts "The current animal is #{animal}"
break if count == 10
count += 1
raise if animal == 'horse'
rescue Exception
retry
end
end
and here is the input I get in the console :
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
Instead of :
The current animal is dog
The current animal is cat
The current animal is horse
The current animal is dog
The current animal is cat
The current animal is horse
The current animal is dog
The current animal is cat
The current animal is horse
The current animal is dog
The current animal is cat
I'm using Ruby 2.1.3
Do you know why it is not working ?
Thanks a lot
Antoine Boillot
10,466 PointsI just tried something else :
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
And it works fine now.
Unsubscribed User
3,309 PointsChris Upjohn and Antoine Boillot, I read on SO that
rescue Exception
is a (very) bad idea. It's worth a look. It includes the intriguing quote:
"Don’t rescue Exception. EVER. or I will stab you."
http://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby
Much better to use
rescue
Since the default is rescue StandardError
To quote Daniel Fone: "So…
… if you encounter rescue Exception => e in an existing codebase, you can almost certainly replace it with rescue => e.
… if you find yourself about to type rescue Exception => e, slap yourself in the face, figure out exactly what exceptions you’re dealing with and rescue those instead."
http://daniel.fone.net.nz/blog/2013/05/28/why-you-should-never-rescue-exception-in-ruby/