From: Gavin Kistner Date: 2004-02-15T03:29:59+09:00 Subject: Re: noobie question - module/class vs obj Paul Vudmaska wrote: >In simular situations w/javscript i do this: > instead of: > function class(){ > this.prop = 0 > > } > obj = new class > > ... I use > > obj = { > prop : 0 > } > > Which is kinda what i'm shooting for in Ruby. Well, in JS that's creating an Object literal, rather than creating a class. The equivalents in ruby would be either a Hash literal: obj = { 'prop' => 0 } or a Singleton design. See: http://phrogz.net/ProgrammingRuby/frameset.asp?content=intro.asp%23arraysandhashes and http://phrogz.net/ProgrammingRuby/frameset.asp?content=language.asp%23hashes for information on Hashes, and http://phrogz.net/ProgrammingRuby/frameset.asp?contenttut_classes.asp%23singletonsandotherconstructors and http://phrogz.net/ProgrammingRuby/frameset.asp?content=lib_patterns.asp%23singleton for information on Singletons. > 2) I thot i would include a module with some generic class methods > into a class ie: > module Somemodule > self.boo > puts 'boo' > end > end > class SomeClass > include Somemodule > > self.boohoo > # puts boo #dies cant find SomeClass method boo > puts Somemodule.boo #works ok > end > end If you're talking about instance methods, you want: module Somemodule def boo puts 'boo' end end class SomeClass include Somemodule def boohoo boo end end If you're talking about class methods, then you want: module Somemodule def self.boo puts 'boo' end end class SomeClass include Somemodule def self.boohoo self.class.boo end end