R
Raj Singh
Based on the following example should I conclude whether I use
instance_eval or class_eval, define_method always creates an instance
method.
class Music
end
Music.instance_eval do
define_method :jazz do
puts 'i am jazz'
end
end
Music.class_eval do
define_method :rock do
puts 'i am rock'
end
end
Music.jazz rescue puts 'Music.jazz failed'
Music.rock rescue puts 'Music.rock failed'
Music.new.jazz rescue puts 'Music.new.jazz failed'
Music.new.rock rescue puts 'Music.new.rock failed'
output is:
Music.jazz failed
Music.rock failed
i am jazz
i am rock
instance_eval or class_eval, define_method always creates an instance
method.
class Music
end
Music.instance_eval do
define_method :jazz do
puts 'i am jazz'
end
end
Music.class_eval do
define_method :rock do
puts 'i am rock'
end
end
Music.jazz rescue puts 'Music.jazz failed'
Music.rock rescue puts 'Music.rock failed'
Music.new.jazz rescue puts 'Music.new.jazz failed'
Music.new.rock rescue puts 'Music.new.rock failed'
output is:
Music.jazz failed
Music.rock failed
i am jazz
i am rock