R
Robert Klemme
Ilias Lazaridis said:Robert Klemme wrote:
[...]
[...][...]So, the last question for this part:
how can I add runtime-accessible [meta]data to a ruby function
definition?
I don't understand.
def talker
def sayHello
puts "Hello World"
end
def sayYourName
puts @name
end
end
how do I put a "hash" which keeps metadata to each _function_?
class Module
def meta() @meta ||= {} end
end
class Foo
def bar() end
meta[:bar] = "bar_meta"
end
the above is essentially "class metadata".
but it gives me the basic idea:
class Object
def meta() @meta ||= {} end
end
talker.sayYourName.meta[:author] = "it's just me"
puts talker.sayYourName.meta[:author]
=> it's just me
[i like this language *very* much]
-
But to understand this fully, can someone please decrypt this:
def meta()
@meta ||= {}
end
If @meta is something false (i.e. nil or false) then the expression on the
right hand side is evaluated and the result is assigned to @meta. It's
short for any of these
@meta = {} unless @meta
@meta || (@meta = {})
@meta or @meta = {}
{} creates a hash (synonym for Hash.new, but you can also put values there
like {"foo"=>"bar"}).
HTH
robert