M
Michael Lesniak
Hello,
I thought the difference between do...end and {...} is only
syntactical, but:
--- code ---
class Object
# For each instance of Object,
# -> for each thing in ruby,
# a method is defined.
def metaclass
class << self
self
end
end
# Executes in the context of
# an instance of something
def meta_eval &blk
metaclass.instance_eval &blk
end
# Adds methods to a metaclass,
# i.e. the instantiated object
# itself
def meta_def name, &blk
meta_eval do
define_method(name, &blk)
end
end
end
--- end ---
The code
a = "foo"
a.meta_def :foo do puts "hi" end
a.foo
functions, but
a = "foo"
a.meta_def :foo { puts "hi" }
does not (even tried different styles, e.g. putting some komma in,
etc...)
A bit stuck but thankful for the ruby community for the answers given
so far,
Michael
I thought the difference between do...end and {...} is only
syntactical, but:
--- code ---
class Object
# For each instance of Object,
# -> for each thing in ruby,
# a method is defined.
def metaclass
class << self
self
end
end
# Executes in the context of
# an instance of something
def meta_eval &blk
metaclass.instance_eval &blk
end
# Adds methods to a metaclass,
# i.e. the instantiated object
# itself
def meta_def name, &blk
meta_eval do
define_method(name, &blk)
end
end
end
--- end ---
The code
a = "foo"
a.meta_def :foo do puts "hi" end
a.foo
functions, but
a = "foo"
a.meta_def :foo { puts "hi" }
does not (even tried different styles, e.g. putting some komma in,
etc...)
A bit stuck but thankful for the ruby community for the answers given
so far,
Michael