From: Ilias Lazaridis Date: 2005-03-21T02:59:53+09:00 Subject: Re: [EVALUATION] - E03 - jamLang Evaluation Case Applied to Ruby Martin DeMello wrote: > Ilias Lazaridis wrote: > >>The above thread showed finally the need to split the evaluation down >>into smaller chunks. >> >>Here is a simple evaluation template (first part) which can be applied >>to the Ruby language: >> >>http://lazaridis.com/case/lang/index.html > > Okay, that's a neat bit of work. Here's a quick solution (ruby makes > most of this fairly trivial): I apologize for the late answer. > # here's your basic talker class > > class Talker > def sayHello > puts "Hello world" > end > end ok > # __FILE__ == $0 means that the program is being run directly > # rather than 'require'd from another file > > if __FILE__ == $0 > talker = Talker.new > talker.sayHello > end Assuming I placethe code into the file "talker.rb". from the command-line, i like to start it, e.g. with "ruby talker.rb" I miss a "main" directive / function / object. something like: def main talker = Talker.new talker.sayHello end > # one-pass interpreter, and you can reopen classes > # so let's just continue [sidenote: I don't understand this] > # here are the two instance variables added to the class, complete with > # autogenerated getters and setters very nice! > class Talker > attr_accessor :name, :age can I write?: attr_accessor :name attr_accessor :age > def initialize(name, age) > @name, @age = name, age > end Is this the constructor? I assume I can write def initialize(name, age) @name = name @age = age end > # following the spec, though say_name is more rubyish > def sayYourName > puts @name > end can I write?: def sayYourName puts @name end > def sayYourAge > puts @age > end > end ok > # now note that our object 'talker' has access to the new methods in its > # class > > if __FILE__ == $0 > # note that we aren't breaking encapsulation and accessing the vars directly; > # the setter methods are called name= and age=, and the getters are called > # name and age very(!) nice! > talker.name = "Just another talker" > talker.age = 1 > talker.sayYourName > talker.sayYourAge > end ok > # reflection [...] seperate message will follow. .. -- http://lazaridis.com