7
7stud --
Here is some code that works as expected:
def create_class
Class.new
end
MyClass = create_class
MyClass.class_eval do
def greet
puts 'hi'
end
end
obj = MyClass.new
obj.greet
--output:--
hi
Now here is a more complex version:
def create_class
cl = Class.new
cl.class_eval do
yield
end
return cl
end
MyClass = create_class do
def greet
puts 'hello'
end
end
obj = MyClass.new
obj.sendgreet)
#obj.greet #private method -- NoMethodError
Why is greet() a private method?
def create_class
Class.new
end
MyClass = create_class
MyClass.class_eval do
def greet
puts 'hi'
end
end
obj = MyClass.new
obj.greet
--output:--
hi
Now here is a more complex version:
def create_class
cl = Class.new
cl.class_eval do
yield
end
return cl
end
MyClass = create_class do
def greet
puts 'hello'
end
end
obj = MyClass.new
obj.sendgreet)
#obj.greet #private method -- NoMethodError
Why is greet() a private method?