noobie question - module/class vs obj

P

Paul Vudmaska

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
 
G

Gavin Kistner

Paul said:
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#arraysandhashes
and
http://phrogz.net/ProgrammingRuby/frameset.asp?content=language.asp#hashes

for information on Hashes, and

http://phrogz.net/ProgrammingRuby/frameset.asp?contenttut_classes.asp#singletonsandotherconstructors
and
http://phrogz.net/ProgrammingRuby/frameset.asp?content=lib_patterns.asp#singleton

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
 
G

Gavin Kistner

Gavin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,142
Messages
2,570,820
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top