tag:blogger.com,1999:blog-8341844892564516905.post4337225323335872183..comments2023-11-02T01:22:19.726-07:00Comments on Ruby Eye for the Java Guy: Singleton Classes in RubyUnknownnoreply@blogger.comBlogger3125tag:blogger.com,1999:blog-8341844892564516905.post-10969659666021733532012-11-15T05:03:05.160-08:002012-11-15T05:03:05.160-08:00check out http://adprog.blogspot.com/ for a table ...check out http://adprog.blogspot.com/ for a table on method locations depending on whether they are instance, class or singleton methods.Anonymoushttps://www.blogger.com/profile/14464852793778826923noreply@blogger.comtag:blogger.com,1999:blog-8341844892564516905.post-62596701963049296012009-07-30T08:45:41.692-07:002009-07-30T08:45:41.692-07:00Note that to retrieve a singleton_class you can ru...Note that to retrieve a singleton_class you can run: (class << hello; self; end)Roger Packhttps://www.blogger.com/profile/01578246846716577925noreply@blogger.comtag:blogger.com,1999:blog-8341844892564516905.post-60672307062223979102008-05-29T17:41:00.000-07:002008-05-29T17:41:00.000-07:00Nice post!Unless I'm missing something, the key to...Nice post!<BR/><BR/>Unless I'm missing something, the key to understanding the chunk of code in question is that singleton classes can have *both* methods and instance variables. <BR/><BR/>The singleton class for a class like Array gets inserted between the Array class and it's parent, the Class class.<BR/><BR/>The singleton class for a class of my own, say C, gets inserted between the C class and the Object class. <BR/><BR/>Singleton classes can have both methods and instance variables just like any old class. The attr_accessor :some_instance_variable sets up a getter method and a setter method for some_instance_variable.<BR/><BR/>Let's change the code chunk:<BR/><BR/>##############<BR/>class C<BR/> class << self<BR/> attr_accessor :some_instance_variable<BR/> end<BR/>end<BR/><BR/>C.some_instance_variable = "bladiblah" <BR/>puts C.some_instance_variable #=> "blahdiblah"<BR/>###############<BR/><BR/>The attr_accessor just creates a getter and setter for the instance variable so, this is the same thing as the following:<BR/><BR/>##############<BR/>class C<BR/> class << self<BR/> <BR/> def some_instance_variable<BR/> @some_instance_variable<BR/> end<BR/> <BR/> def some_instance_variable= (new_iVar)<BR/> @some_instance_variable = new_iVar<BR/> end<BR/> end<BR/>end<BR/><BR/>C.some_instance_variable = "bladiblah" <BR/>puts C.some_instance_variable # =>"blahdiblah"<BR/>###############<BR/><BR/>So, this is all pretty standard. The fascinating thing is that you can do it with those weird creatures--singleton classes.<BR/><BR/>Now, let's add a sub-class and try the same thing:<BR/><BR/>##############<BR/>class D < C<BR/>end<BR/>D.some_instance_variable = "bladiblah" <BR/>puts D.some_instance_variable # =>"blahdiblah"<BR/>##############<BR/><BR/>Why does *this* work? Because when the code is executed the compiler looks at the D class and doesn't find the setter method, some_instance_variable=, then it goes up one level and looks at its parent, the C class, and finds no setter method. Then it goes up another level and looks at the C singleton class. There it does find the setter method. Same thing happens with the getter method.Unknownhttps://www.blogger.com/profile/16037736758479487186noreply@blogger.com