From: Robert Klemme Date: 2005-04-03T21:19:43+09:00 Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby "Ilias Lazaridis" schrieb im Newsbeitrag news:d2oki2$4m$1@usenet.otenet.gr... > 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