Broken alias behavior in Ruby 1.9.2?

T

Tron Fu

The following code works in Ruby 1.8.7 but not in 1.9.2, instead, I get
the error:
NoMethodError: undefined method `old_encode64' for Base64:Module

Can someone please shed some light on this.

Thanks in advance,
Tron

module Base64
alias :eek:ld_encode64 :encode64
module_function # this seems to be necessary -- get a "wrong number of
arguments (2 for 1)" without it this line.

def encode64(bin,options={})
if (options[:no_line_break] == true)
old_encode64(bin).gsub(/\n/, '')
else
old_encode64(bin)
end
end
end
 
I

Intransition

The following code works in Ruby 1.8.7 but not in 1.9.2, instead, I get
the error:
NoMethodError: undefined method `old_encode64' for Base64:Module

Can someone please shed some light on this.

Thanks in advance,
Tron

module Base64
=A0 alias :eek:ld_encode64 :encode64
=A0 module_function # this seems to be necessary -- get a "wrong number o= f
arguments (2 for 1)" without it this line.

=A0 def encode64(bin,options=3D{})
=A0 =A0 if (options[:no_line_break] =3D=3D true)
=A0 =A0 =A0 old_encode64(bin).gsub(/\n/, '')
=A0 =A0 else
=A0 =A0 =A0 old_encode64(bin)
=A0 =A0 end
=A0 end
end

Your alias is only creating an instance method, yet you are using it
as a class method.

To be sure I would need to look at the original code, but presumely

module Base64
class << self
=A0 alias :eek:ld_encode64 :encode64
end
end
 
T

Tron Fu

Thank you for your reply. However, your suggestion resulted in
SystemStackError: stack level too deep

I'm guessing that it is not working in 1.9.2 because Base64 is no longer
included with 1.9 and there is something odd going on with the
ActiveSupport's version of it.

Tron
 
M

Michael Morin

Thank you for your reply. However, your suggestion resulted in
SystemStackError: stack level too deep

I'm guessing that it is not working in 1.9.2 because Base64 is no longer
included with 1.9 and there is something odd going on with the
ActiveSupport's version of it.

Tron

As far as I can tell, 1.9.2 ships with Base64.

I suspect you're having trouble because of Base64's use of
module_function. This creates copies of the original methods, so it
will be difficult to overload like this.

--
Michael Morin
Guide to Ruby
http://ruby.about.com/
Become an About.com Guide: beaguide.about.com
About.com is part of the New York Times Company
 
T

Tron Fu

Thanks, Michael.

I ended up not using and just reimplemented the base64 encode as
[bin].pack("m")

Tron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top