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