tag:blogger.com,1999:blog-12467669.post5817659194964117561..comments2023-04-29T07:23:25.825-04:00Comments on Jay Fields' Thoughts: Ruby: Class MethodsJayhttp://www.blogger.com/profile/14491442812573747680noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-12467669.post-60599772056806670312010-04-21T08:17:35.625-04:002010-04-21T08:17:35.625-04:00Srinidhi,
I believe you're misunderstanding ho...Srinidhi,<br />I believe you're misunderstanding how instance variables are set. Also, when I run your example in irb I don't get the same results you have listed in the comment.<br /><br />Here are a few examples of how instance variables work and how the context in which they are defined makes a difference.<br /><br />class HasInstanceVariables<br /> def initialize<br /> @a_var = 1<br /> end<br />end<br /><br />The above example is a class that will have an instance variable set when constructed. Notice that the @a_var is being set in the context of the instance, not in the context of the class.<br /><br />class HasSingletonClassInstanceVariables<br /> @a_var = 1<br />end<br /><br />The previous example is setting an instance variable on the singleton class. This doesn't seem to be the behavior you are looking for.<br /><br />Lastly, here's another example of both types of variables within the same class.<br /><br />irb(main):039:0> class BothVars<br />irb(main):040:1> @singleton_instance_variable = 1<br />irb(main):041:1> <br />irb(main):042:1* def initialize<br />irb(main):043:2> @instance_variable = 2<br />irb(main):044:2> end<br />irb(main):042:1><br />irb(main):047:1> def self.singleton_var<br />irb(main):048:2> @singleton_instance_variable<br />irb(main):049:2> end<br />irb(main):050:1> <br />irb(main):051:1* def instance_var<br />irb(main):052:2> @instance_variable<br />irb(main):053:2> end<br />irb(main):054:1> end<br />=> nil<br />irb(main):055:0> BothVars.singleton_var<br />=> 1<br />irb(main):056:0> BothVars.new.instance_var<br />=> 2Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12467669.post-66716217752410942242010-04-21T05:44:30.772-04:002010-04-21T05:44:30.772-04:00Jay Fields,
This is Srinidhi.I think it’s...Jay Fields,<br /> This is Srinidhi.I think it’s very good approach to make everybody understand about class methods and instance methods, however while I learnt this, some of my variable concepts got haywire and I m feeling very bad as how come I got into such basic doubts.<br />Any way here is my problem in variables: Instance Var:<br />class A<br />@a=1<br /><br />def a1<br />puts @a=1<br />end<br /><br />def a2<br />@a=2<br />puts @a<br />end<br /><br />end<br /><br />For this:<br />x=A.new<br />x.a1 == > nil # As expected as @a=1 defined is class level not method level. And once instance var is set in method it is reflected in all methods.<br />x.a2 == > #@a is set to 2.<br />x.a1 == > 2 # As expected.<br /><br />However if I use class methods:<br />class A<br />@a=1<br /><br />def self.a1<br />puts @a<br />end<br /><br />end<br /><br />For this:<br />A.a1 == > 1 # OOOPS!!!! How is this possible. The instance var(@a) is set at class level, how did it enter method level. It should have been nil right, as per previous example.<br /><br />Also:<br />class A<br /><br />def a1<br />@a=2<br />end<br /><br />def a2<br />puts @a<br />end<br /><br />end<br /><br />#lets create a child class B<br />class B < A<br />@a=”Srinidhi”<br />def self.b1<br />puts @a<br />end<br /><br />def b2<br />a=A.new<br />puts @a<br />end<br /><br />def b3<br />puts @a<br />end<br /><br />end<br /><br />For this:<br />x=B.new<br />x.a2 == >nil #Ofcourse it is nil as it is not yet set.<br />x.a1 == > @a is set to 2<br />x.a2 == >2 #As expected.<br />x.b3 == > 2 #OOOPS!! How is this possible. @a is an instance variable , how did it pass across classes . If so why do I need to use a class var if instance var alone can achieve this job.<br />x.b2 == > Also gives output 2. This is expected as I m instantiating A and thus calling all var values temporarily in that method<br />B.b1 == > Gives “Srinidhi”.Srinidhihttps://www.blogger.com/profile/01582446664844080741noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-32946660277542730852008-06-16T09:55:00.000-04:002008-06-16T09:55:00.000-04:00I like comparisons.. include as many platform as y...I like comparisons.. include as many platform as you want because it is interesting to find diffirentces..Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12467669.post-9624800748394994892007-04-30T00:44:00.000-04:002007-04-30T00:44:00.000-04:00Unless I am mistaken, Ruby class methods are polym...Unless I am mistaken, Ruby class methods <I>are</I> polymorphic. If you call 'self.class.foo', if no message is found, the message gets sent to the class object's parent.<BR/><BR/>Class objects in Ruby (unlike Java) can have private instance variables (@) in addition to class variables (@@). Given that, why do they break the data + code abstraction?Ara Vartanianhttps://www.blogger.com/profile/12592706325147338463noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-22664358187313119152007-04-21T02:34:00.000-04:002007-04-21T02:34:00.000-04:00The main problem with static methods are not thei...The main problem with static methods are not their lack of polymorphism, etc but the faact that they encourage thinking about computation as behavior operating on data. This encourages the separation of the two and leads to thin objects which dont have behavior or miss whole new abstractions altogether. Of course all this is true only if you are working in a language with OO paradigm.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12467669.post-80313206006134596332007-04-20T21:57:00.000-04:002007-04-20T21:57:00.000-04:00Re static methods are not polymorphic: rather tha...Re static methods are not polymorphic: rather than saying it's not revelant, I would say that class methods in Ruby <I>are</I> polymorphic. That's fundamental difference, as you mention later: class methods in Ruby are fully object-oriented, polymorphic methods of objects of type Class. In Java static methods are not even object-oriented, because they are not associated with any object; they're just like functions in a procedural language like C.Steve Molitorhttps://www.blogger.com/profile/07269694833796718686noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-43228568172261445712007-04-20T16:44:00.000-04:002007-04-20T16:44:00.000-04:00I know what you mean about C#. I used to follow th...I know what you mean about C#. I used to follow the rule of no static methods, so even when a method didn't need instance variables, I would instantiate a useless object and call the method on that. I figured I was being "diligent".<BR/><BR/>With Ruby I've embraced class methods. If a method uses no instance variables, then I make it a class method. Super easy rule to follow.Unknownhttps://www.blogger.com/profile/13452316141320859216noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-61539406093749605832007-04-20T16:00:00.000-04:002007-04-20T16:00:00.000-04:00Giles,I agree that static methods in Java are comp...Giles,<BR/>I agree that static methods in Java are completely different than Ruby singleton methods. Unfortunately, people new to Ruby often mistake them for the same thing.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-12467669.post-83201076051376738872007-04-20T14:51:00.000-04:002007-04-20T14:51:00.000-04:00The real stumbling block is that Java class method...The real stumbling block is that Java class methods are totally different things from Ruby "class" methods, so different that applying Java rules of thumb to Ruby just isn't even relevant. If you can get a Java programmer to see the difference in what classes are, the class methods stumbling block vanishes.<BR/><BR/>Didn't include C# because I have no experience with it, but I believe C# has an object/classes model virtually identical to Java's, so this may be equally relevant there as well. It's partly just that knowing OOP is very different from knowing Java's OOP, or Ruby's OOP. Ruby's OOP runs on a Smalltalk paradigm which is very different from Java's.Giles Bowketthttps://www.blogger.com/profile/06462574543661010181noreply@blogger.comtag:blogger.com,1999:blog-12467669.post-63129674742006254812007-04-20T13:17:00.000-04:002007-04-20T13:17:00.000-04:00The problem I have with static/class methods are w...The problem I have with static/class methods are with some "programmers" that use them. I've worked with many "programmers" that do imperative programming via a ton of class methods while believing they are doing OO.Anonymousnoreply@blogger.com