From: Gabriel Dragffy Date: 2007-08-03T00:30:04+09:00 Subject: Re: Beginner's tutorial and poor attempt at classes On 2 Aug 2007, at 15:53, Drew Olson wrote: > Gabriel Dragffy wrote: >> I still struggling >> with trying to identify the differences between self.???, @, and @@. > > Gabriel - > > I'll take a crack at trying to help you with these symbols. > > @ - this designates an instance variable within an object. The > value of > this variable differs between instances of the same class. > > @@ - this is a class variable. The value of this variable is the same > between ALL instances of the same class. > > self.* - this is used to declare class methods. You call these methods > directly on a class rather than using a instance of a class. > > Below is a little irb session to demonstrate things a bit more: > > irb(main):001:0> class Foo > irb(main):002:1> def initialize > irb(main):003:2> @inst = 0 > irb(main):004:2> @@cls = 0 > irb(main):005:2> end > irb(main):006:1> def increment_instance > irb(main):007:2> @inst += 1 > irb(main):008:2> end > irb(main):009:1> def increment_class > irb(main):010:2> @@cls += 1 > irb(main):011:2> end > irb(main):012:1> def print_stats > irb(main):013:2> puts "class var: #{@@cls}, instance var: # > {@inst}" > irb(main):014:2> end > irb(main):015:1> def self.say_hello > irb(main):016:2> puts "Hello from a class method!" > irb(main):017:2> end > irb(main):018:1> end > => nil > irb(main):019:0> > irb(main):020:0* one_foo = Foo.new > => # > irb(main):021:0> two_foo = Foo.new > => # > irb(main):022:0> > irb(main):023:0* 2.times{ one_foo.increment_instance } > => 2 > irb(main):024:0> one_foo.increment_class > => 1 > irb(main):025:0> > irb(main):026:0* one_foo.print_stats > class var: 1, instance var: 2 > => nil > irb(main):027:0> > irb(main):028:0* two_foo.increment_instance > => 1 > irb(main):029:0> two_foo.increment_class > => 2 > irb(main):030:0> > irb(main):031:0* two_foo.print_stats > class var: 2, instance var: 1 > => nil > irb(main):032:0> > irb(main):033:0* Foo.say_hello > Hello from a class method! > => nil > > Welcome to ruby! (it's wayyyy better than python :) WOW! What a reply! This has got to be the single best thing for Ruby - the peeps on this mailing list are awesome!! Very dry, unwelcoming, and sometimes rude on the Python list - doesn't apply to everyone on there, some peeps are dashing, but the majority... Anyway. That example does make it somewhat clearer, thank you. I think that all in all I do understand what these things are and what they do, however, because I haven't come across them before I just feel a little unsure when to use them, or even when it's best to use them. Oh one other thing I am liking more about ruby than python is the different approaches taken by the languages. I'm starting to have warm feelings for Ruby because it's more... human. I can write the code using blocks, on one line, or whatever I deem best for that particular item. I thought the compulsory white space in Python was good, but it makes coding very 'rigid'. Well anyway, enough about this Python vs Ruby thing, I obviously don't need to harp on about it to the people on this list - you've already settled in the newfoundland! I'm looking forward to the version of Ruby that gives you a .rbc :) then things will be even faster!