Noobie question - module/class vs obj

Hi all,
Couple questions

  1. I’m still pretty new to ruby.In my code i’m using quite a few classes
    and i do not need multiple instances of most of them. So in lieu of
    creating
    a global object $obj = new SomeClass, i simply use the Class and
    it’s class methods/atts directly. Is this ok/good practice? Should I
    just use these as modules in this case?
    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.

  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

Should’nt the include pull those class meths into SomeClass?Am i causing
this issue/confusion by not intantiating objects from these classes?
Shoul I make this class
a module. I think i’ll go try that…hmmm.

Thanks for any inight,paul

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.

  1. 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

Gavin Kistner wrote:

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

Oops…I thought that would work. Frankly…I’m a beginner to, and
apparently I don’t know how (or if) you can call an included class
method as part of a ‘native’ class method.