Sure it's in there just not in so many words :-)
eval ("class String; alias plus + ; end")
def on ; 1 ; end
def off ; 2; end
def conversion (x)
if x==1
eval "class String; def +(x); plus x.to_s; end ; end"
elsif x==2
eval "class String; def +(x); plus x; end; end"
else
raise "unknown setting #{x}"
end
end
conversion on
puts "x" + 1
conversion off
puts "x" +1
This is not that clean really, because playing with this caused a bit of a
problem.
First I just 're-aliased' the function ,that lead to a recursive loop.
So I instinctively wrote
eval ("class String; alias plus + ; end") unless String.methods.include?
"plus"
But no, the aliased function doesn't show up as a method either. So is
there any way to tell what functions have been aliased?
Ralph
----- Original Message -----
From: "Dave Thomas" <Dave / PragmaticProgrammer.com>
To: "ruby-talk ML" <ruby-talk / ruby-lang.org>
Sent: Thursday, November 22, 2001 12:27 PM
Subject: [ruby-talk:26089] Re: string concat and .to_s
> "mark hahn" <mchahn / facelink.com> writes:
>
> > > I frankly haven't tried running Ruby extensively with
> > > autoconversion turned on
> >
> > Oh! I didn't know there was such a feature. Is this in the pickaxe
> > book?
>
> No - it isn't built in. I mean enabling it by extending the
> appropriate classes.
>
>
> Dave
>