Sha256: 721102f179e099c07a9ccf94bdac72c2a3e69c59bbbc821818e1a0b0af74646e
Contents?: true
Size: 1.18 KB
Versions: 26
Compression:
Stored size: 1.18 KB
Contents
class Module # Redirect methods to other methods. This simply # defines methods by the name of a hash key which # calls the method with the name of the hash's value. # # class Example # redirect_method :hi => :hello, :hey => :hello # def hello(name) # puts "Hello, #{name}." # end # end # # e = Example.new # e.hello("Bob") #=> "Hello, Bob." # e.hi("Bob") #=> "Hello, Bob." # e.hey("Bob") #=> "Hello, Bob." # # The above class definition is equivalent to: # # class Example # def hi(*args) # hello(*args) # end # def hey(*args) # hello(*args) # end # def hello # puts "Hello" # end # end # def redirect_method( method_hash ) method_hash.each do |targ,adv| define_method(targ) { |*args| send(adv,*args) } end end end # _____ _ # |_ _|__ ___| |_ # | |/ _ \/ __| __| # | | __/\__ \ |_ # |_|\___||___/\__| # =begin test require 'test/unit' class TCModule < Test::Unit::TestCase def red1 ; 1 ; end redirect_method :red2 => :red1 def test_redirect assert_equal( 1, red2 ) end end =end
Version data entries
26 entries across 26 versions & 1 rubygems