From: why the lucky stiff Date: 2002-12-02T02:20:19+09:00 Subject: Re: [FAQ] What do some of Ruby's symbols mean? Shannon Fang (xrfang@hotmail.com) wrote: > I'm sorry I can't understand the purpose of your question. Is it related > to understanding class << object? The level I'm interested in is for > application developers, why they need to use <<, does it make their life > easier, or...? I did not consider to make the language developer's life > easier.. (sorry for being selfish :-p). The class << self syntax isn't terribly difficult to understand once you actually find a use for it. I usually use it a couple times in many of the applications I write. I find it useful for adding methods which I can use in my class descriptions. For example, I worked on an application which communicated quite a bit with the Windows API. I wrote a wrapper class using class << self. require 'Win32API' class Win32Wrapper # Abstract away the Win32API wrapper class << self def def_api( methName, winName, imp, exp ) class_eval %{ def #{methName}( *a ) Win32API. new( '#{self.name.upcase}', '#{winName}', #{imp.inspect}, #{exp.inspect} ). call( *a ) end } end end end The code is a bit thick, so you might notice that I'm defining a `def_api' method inside of the class << self. Now I call that method in my class definitions: # User32 DLL class User32 < Win32Wrapper def_api :desktop, :GetDesktopWindow, [], 'L' def_api :activeWindow, :GetActiveWindow, [], 'L' end Using the `def_api' method in my class definitions makes them look a lot cleaner. Just using a normal method definition would bloat my classes to look like this: # User32 DLL class User32 < Win32Wrapper def desktop( *a ) Win32API. new( 'USER32', 'GetDesktopWindow', [], 'L' ). call( *a ) end def activeWindow( *a ) Win32API. new( 'USER32', 'GetActiveWindow', [], 'L' ). call( *a ) end end I personally prefer class << self in many situations, because I don't have to litter my code with the class name either. _why