First Law of Classes

M

Mike Stephens

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?
 
J

Joel VanderWerf

If you saw a class without persistent data, would you be suspicious?

Maybe, but there are good uses:

1. instance as unique token

STATUS_OK = Object.new
STATUS_FAIL = Object.new

result =
begin
# try something
rescue
STATUS_FAIL
else
STATUS_OK
end

2. flyweight pattern

class PseudoRandom
def initialize
# set up seed, etc.
end

def next
# get number from seed
end
end

class Random
def next
# get number from /dev/urandom
end
end

# This method accepts *either* of the above classes.
# Even though Random has no state, it supports the same
# interface as Pseudorandom.
def get3 cl
seq = cl.new
[seq.next; seq.next; seq.next]
end
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top