ruby: define_method vs. def


as programming exercise, i've combined flush dash creates class, instantiates twin objects class, monkeypatches object, relies method_missing monkeypatch one.



here's deal. works intended:



class monkey

def chatter
puts "i am chattering monkey!"
end

def method_missing(m)
puts "no #{m}, i'll one..."
def screech
puts "this new screech."
end
end
end

m1 = monkey.new
m2 = monkey.new

m1.chatter
m2.chatter

def m1.screech
puts "aaaaaargh!"
end

m1.screech
m2.screech
m2.screech
m1.screech
m2.screech


you'll notice i have parameter method_missing. i since i anticipating define_method boldly emanate blank methods suitable name. however, doesn't work. fact, even controlling define_method stationary name so:



def method_missing(m)
puts "no #{m}, i'll one..."
define_method(:screech) do
puts "this new screech."
end
end


ends following result:



argumenterror: wrong array arguments (2 1)

method method_missing untitled request during line 9
method method_missing untitled request during line 9
at tip turn untitled request during line 26
program exited.


what creates blunder summary some-more bewildering i wholly have justification method_missing...



Comments

Popular posts from this blog

why does floated <input> control floated component slip over too distant right ie7, nonetheless firefox?

grails record upload problems

how i emanate permitted url asp.net mvc?