Phrogz
9/6/2007 9:23:00 PM
On Sep 6, 3:15 pm, Helgitomas Gislason <nitrohel...@hotmail.com>
wrote:
> The main point I'm trying to get to here is: "Is there any function that
> I can put at the beginning or the end, that says my program to exit
> whenever I say exit WITHOUT having to put the:
>
> if name == 'exit'
> exit
> end
>
> at every string I want to have the possibility yo exit from??
class Object
def gets( *args )
result = super
exit if result =~ /\A\s*exit\s*\z/oi
result
end
end
However, instead of monkey-patching the object class (and shadowing)
the Kernel#gets method), I would suggest instead writing your own
method:
def gets_or_exit
result = gets
exit if result =~ /\A\s*exit\s*\z/oi
result
end
This gives you the ability to decide if there's a time when you want
to use gets without exiting, and to do additional common work (like
always #chomp the result before returning it).