I was introduced to Ruby by a charming German girl called Ulrika. She
was writing Watir programs for a vendor product.
On an old computer, I just found a program she had written. It scripts a
quote-and-buy insurance web site.
She has a ‘main’ program that calls a class that contains a series of
procedural methods working through the screens. Apart from that she has
a class that generates random input.
As a procedural program it is perfectly readable. However both of these
two classes contain no class or instance data. They either refer to
globals or receive parameters.
Now I think there ought to be a rule that a class is not a class if it
only has methods. It might be a module if you want to wrap it neatly.
If you saw a class without persistent data, would you be suspicious?
Now I think there ought to be a rule that a class is not a class if it
only has methods. It might be a module if you want to wrap it neatly.
Technically, if it’s just procedures with no mixins, there’s no
particular reason for it to be a module either.
example 1
module Foo
def self.foo
puts “Method in module”
end
end
example 2
Bar = Object.new
def Bar.bar
puts “Method in object”
end
Foo.foo
Bar.bar
If you saw a class without persistent data, would you be suspicious?
Not really. A class is_a module anyway. It’s neater to use module just
to document that you don’t expect any instances to be created.
I would avoid the global variables though, and instead use instance
variables of the class/module/whatever container. That makes them a bit
less ‘global’.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.