print "Enter a number: " n = gets.to_i begin result = 100 / n rescue puts "Your number didn''t work. Was it zero???" exit end puts "100/#{n} is #{result}." In the above example, how come the result is being used outside the begin - end block? How does it still remain in scope? TIA. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Am 07.10.2006 um 09:55 schrieb Bala Paranj:> > print "Enter a number: " > n = gets.to_iresult = 0> begin > result = 100 / n > rescue > puts "Your number didn''t work. Was it zero???" > exit > end > puts "100/#{n} is #{result}."Regards Florian --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On Oct 7, 2006, at 12:55 AM, Bala Paranj wrote:> > print "Enter a number: " > n = gets.to_i > begin > result = 100 / n > rescue > puts "Your number didn''t work. Was it zero???" > exit > end > puts "100/#{n} is #{result}." > > In the above example, how come the result is being used outside the > begin - end block? How does it > still remain in scope? TIA.begin..end blocks do not create a new scope but use the existing one. They are mainly a construct to deliminate a block of code that can throw an error and need to be rescued or retried. -Ezra --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
No, you don''t need to declare the result variable before you begin to use it. It just springs up into existence inside the block. --- Florian Aßmann <florian.assmann-htSm2yLGOjU@public.gmane.org> wrote:> > > Am 07.10.2006 um 09:55 schrieb Bala Paranj: > > > > > print "Enter a number: " > > n = gets.to_i > > > result = 0 > > > > begin > > result = 100 / n > > rescue > > puts "Your number didn''t work. Was it zero???" > > exit > > end > > puts "100/#{n} is #{result}."--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---