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>t = MD5=>Digest::MD5>t.md5(''confused'')=>1a7f2a5ad77128b2f81feddac78df213 # so far so good, now start new command line # or unload module>require ''digest/md5''=>true>Digest::MD5.md5(''confused'')=>NoMethodError #shouldn''t this work? bit confused how the package # and methods sit together. -- Posted via http://www.ruby-forum.com/.
> 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'' > =>trueNo problem here> > >t = MD5 > =>Digest::MD5Here 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'') > =>1a7f2a5ad77128b2f81feddac78df213No problem> > # so far so good, now start new command line > # or unload module > > >require ''digest/md5'' > =>trueAgain no problem here.> > >Digest::MD5.md5(''confused'') > =>NoMethodErrorYou''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
On Wed, May 24, 2006 at 02:12:23PM +1000, Ross Dawson 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
> 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.Oops being new to Ruby shows out here and Greg has pointed me in the right direction.> 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.Again oops. The equivalent should have been Digest::MD5.new(''not quite so confused now'') So based on Greg''s details I would think the equivalents would be:- require ''md5'' => true my_md5 = MD5.md5(''clearer'') => ac27b86fdde5ffd474a51a0d7715dc56 my_md5.digest => "\254''\270o\335\345\377\324t\245\032\rw\025\334V" and require ''digest/md5'' => true my_md5 = Digest::MD5.new(''clearer'') => ac27b86fdde5ffd474a51a0d7715dc56 my_md5.digest => "\254''\270o\335\345\377\324t\245\032\rw\025\334V" /usr/lib/ruby/1.8/md5.rb aliases the class Digest::MD5 to MD5 and adds the Class method MD5.md5 which is implemented as an alias to the constructor new.> > This is both patronizing and involves a certain amount of the > pot calling > the kettle black. Yeesh.Next time I''ll fire up IRB and check my results before posting. Thanks for pointing out my errors and helping me learn. Ross