[Ruby] copying methods around
Ryan Davis
ryand-ruby at zenspider.com
Tue Mar 20 14:09:09 PST 2007
#!/usr/local/bin/ruby -w
require 'ostruct'
class PhilCollins
def sussudio
"I'm in #{self.class}"
end
def self.easy_lover
os = OpenStruct.new
class << os
# this was as close as I could get. I don't think it is possible
# since the classes don't match. Ruby will be pretty strict
# about that, but there are ways to cheat... See below.
self.send :define_method, :sussudio,
PhilCollins.instance_method(:sussudio)
end
os
end
end
msg = PhilCollins.easy_lover.sussudio rescue "nope"
puts msg
require 'rubygems'
require 'ruby2ruby'
class PeterGabriel
def self.shock_the_monkey
os = OpenStruct.new
class << os
eval PhilCollins.new.method(:sussudio).to_ruby
end
os
end
end
msg = PeterGabriel.shock_the_monkey.sussudio rescue "nope"
puts msg
More information about the Ruby
mailing list