RE: packages, modules


#1

i cant make sense in my own mind what is happening,
so if someone can explain, it would be appreciated.
I did the following on the command line…

require ‘md5’
=>true

No problem here

t = MD5
=>Digest::MD5

Here is where it starts to differ. The difference is between Class and
Object. Here you have created an instance of the Class MD5 and assigned
it to the variable t. Think of a Class as a cookie cutter used to stamp
out instances of objects.

t.md5(‘confused’)
=>1a7f2a5ad77128b2f81feddac78df213
No problem

so far so good, now start new command line

or unload module

require ‘digest/md5’
=>true
Again no problem here.

Digest::MD5.md5(‘confused’)
=>NoMethodError
You’ve asked the MD5 Class if it accepts the message/method .md5. The
class does not have this method but instances of objects made with the
class do. So you could have written.

t = Digest::MD5 # create an instance of the MD5 class
then
t.md5(‘confused prolly more’)

#shouldn’t this work? bit confused how the package

and methods sit together.

Oh and a package is just a logical grouping of source code. It doesn’t
have any direct relationship with methods. You need to focus on the
fundamentals of Object Orientation.

A quick google for Object Oriented tutorial produced the following link
http://www.aonaware.com/OOP1.htm which may or may not help. The concept
you’re looking for is Object orientation (Classes, Objects,
methods/messages, scope, and don’t get too hung up on inheritance yet
it’ll get you into trouble).

How about a couple of “blow your mind” items to think about during or
after you learn some OO concepts. Just don’t get too hung up on these
too early.

Objects have methods but so do Classes too.
In Ruby EVERYTHING is an object, even Classes.

Hope that whets you appetite.

Ross


#2

On Wed, May 24, 2006 at 02:12:23PM +1000, Ross D. wrote:
} > i cant make sense in my own mind what is happening,
} > so if someone can explain, it would be appreciated.
} > I did the following on the command line…
} >
} > >require ‘md5’
} > =>true
}
} No problem here
} >
} > >t = MD5
} > =>Digest::MD5
}
} Here is where it starts to differ. The difference is between Class
and
} Object. Here you have created an instance of the Class MD5 and
assigned
} it to the variable t. Think of a Class as a cookie cutter used to
stamp
} out instances of objects.

You misread this. There is no MD5.new call on that line, just
setting the variable t to the contents of the variable MD5, which is
identical to Digest::MD5. The rest of your explanation is based on this
misreading, unfortunately.

} > >t.md5(‘confused’)
} > =>1a7f2a5ad77128b2f81feddac78df213
} No problem
}
} > # so far so good, now start new command line
} > # or unload module
} >
} > >require ‘digest/md5’
} > =>true
} Again no problem here.
} >
} > >Digest::MD5.md5(‘confused’)
} > =>NoMethodError

Here is the difference. Instead of requiring ‘md5’ he is requiring
‘digest/md5’ which loads a different file. Taking a look at
/usr/lib/ruby/1.8/md5.rb (or equivalent on your system) makes things
very
clear:

require ‘digest/md5’

MD5 = Digest::MD5

class MD5
def self.md5(*args)
new(*args)
end
end

Requiring ‘digest/md5’ actually loads a shared library (i.e. it’s a
native
extension). That native extension does not define Digest::MD5.md5, but
/usr/lib/ruby/1.8/md5.rb adds that method for convenience.

} You’ve asked the MD5 Class if it accepts the message/method .md5. The
} class does not have this method but instances of objects made with the
} class do. So you could have written.
}
} t = Digest::MD5 # create an instance of the MD5 class
[…]

Nononononono. That comment is incorrect. There is no instance creation
occurring on that line.

} Oh and a package is just a logical grouping of source code. It
doesn’t
} have any direct relationship with methods. You need to focus on the
} fundamentals of Object Orientation.
[…]

This is both patronizing and involves a certain amount of the pot
calling
the kettle black. Yeesh.

} Hope that whets you appetite.
} Ross
–Greg