P
Pedro Del Gallego
Hi
Yesterday , i'd made an example for fun, after read [1]. that let me
introduce pre o post behavior to an method with this easy sintax
class Foo
premy_method) { #some_code}
end
but i ve a proble when i try to move the pre and post method to a
module. Can anyone explainme why the pre methods is undefined in the
class ?
thanks in advance.
[1] : http://split-s.blogspot.com/2006/02/design-by-contract-for-ruby.html
My code :
----------------------------------------------------------------------------------------------------------------
Pre and Post method implemented in Song class
class Song
attr_accessor :title
def play
puts "#{@title}"
end
end
class Song
def self.pre (name_method, &condition)
old_method = instance_method(name_method)
define_method(name_method) { |*args|
condition.call
old_method.bind(self).call(*args)
}
end
def self.post (name_method, &condition)
old_method = instance_method(name_method)
define_method(name_method) { |*args|
condition.call
}
end
end
class Song
pre play ) { puts "Playing ..." }
end
song = Song.new
song.title= "Hola song"
song.play
----------------------------------------------------------------------------------------------------------------
Als module implementation : not work undefined method `pre' for
Song:Class (NoMethodError)
class Song
attr_accessor :title
def play
puts "#{@title}"
end
end
module MetaTest
def self.included(mod)
puts "and now MetaTest has been used by #{mod.inspect}..."
end
def self.pre (name_method, &condition)
old_method = instance_method(name_method)
define_method(name_method) { |*args|
condition.call
old_method.bind(self).call(*args)
}
end
def self.post (name_method, &condition)
old_method = instance_method(name_method)
define_method(name_method) { |*args|
condition.call
}
end
end
class Song
include MetaTest
end
class Song
pre play ) { puts "Playing ..." }
end
song = Song.new
song.title= "Hola caracola"
song.play
Yesterday , i'd made an example for fun, after read [1]. that let me
introduce pre o post behavior to an method with this easy sintax
class Foo
premy_method) { #some_code}
end
but i ve a proble when i try to move the pre and post method to a
module. Can anyone explainme why the pre methods is undefined in the
class ?
thanks in advance.
[1] : http://split-s.blogspot.com/2006/02/design-by-contract-for-ruby.html
My code :
----------------------------------------------------------------------------------------------------------------
Pre and Post method implemented in Song class
class Song
attr_accessor :title
def play
puts "#{@title}"
end
end
class Song
def self.pre (name_method, &condition)
old_method = instance_method(name_method)
define_method(name_method) { |*args|
condition.call
old_method.bind(self).call(*args)
}
end
def self.post (name_method, &condition)
old_method = instance_method(name_method)
define_method(name_method) { |*args|
condition.call
}
end
end
class Song
pre play ) { puts "Playing ..." }
end
song = Song.new
song.title= "Hola song"
song.play
----------------------------------------------------------------------------------------------------------------
Als module implementation : not work undefined method `pre' for
Song:Class (NoMethodError)
class Song
attr_accessor :title
def play
puts "#{@title}"
end
end
module MetaTest
def self.included(mod)
puts "and now MetaTest has been used by #{mod.inspect}..."
end
def self.pre (name_method, &condition)
old_method = instance_method(name_method)
define_method(name_method) { |*args|
condition.call
old_method.bind(self).call(*args)
}
end
def self.post (name_method, &condition)
old_method = instance_method(name_method)
define_method(name_method) { |*args|
condition.call
}
end
end
class Song
include MetaTest
end
class Song
pre play ) { puts "Playing ..." }
end
song = Song.new
song.title= "Hola caracola"
song.play