Topic: Another couple of Ruby basics questions...

Ok I have got to the end of my Ruby for Rails Book and there are a few things that I want to make sure I have right in my head before I tackle the Agile Web book, and that is the definitions of instace mthods, instance Variables and class methods.

1. Instance Methods only appear once per class and have a unique set of values?

2. Class Methods can be used anywhere within a class i.e. a class method could be referenced inside another method?

3. Instave variables are unique variables can can only be used by the class they are created in and start with an @ symbol?

Do I have these definitions right and ammendments or clarification would be appreciated. I have read the old pickaxe online docs and some google searches but it didnt really help me understand these terms any better sad

Thanks

Last edited by Opel (2007-01-30 12:43:48)

Re: Another couple of Ruby basics questions...

This probably isn't a technically accurate answer, but it is just how I see the difference between classes and instances.

Think of a class as a blueprint of a house. You can use this blueprint to build as many houses (instances) as you want. They are roughly the same, but there are some differences - for example, the color of the carpet. These differences are stored in instance variables because they change depending upon the instance.

Hmm, let me use Ruby code to help explain this:

class House
  def self.position_of_toilet # this is a class method
    [127, 35]
  end
 
  def initialize # this will be called automatically when we create an instance (build a house)
    @color_of_carpet = "white" # default to white
  end
 
  def color_of_carpet # this is an instance method
    @color_of_carpet
  end
 
  def change_carpet_color(color) # this is another instance method
    @color_of_carpet = color
  end
end

House # this is the blueprint

House.position_of_toilet # returns [127, 35]
House.color_of_carpet # error! Can't call an instance method from a class (blueprint)

house = House.new # make an instance of the class (build a house from the blueprint)
house.color_of_carpet # returns "white"
house.change_carpet_color("green")
house.color_of_carpet # returns "green"
house.position_of_toilet # ack, error! Can't call a class method, you need to look at the blueprint
house.class.position_of_toilet # there we go, this works


Hopefully that made some sense, lol. Does that answer any questions?

Railscasts - Free Ruby on Rails Screencasts

Re: Another couple of Ruby basics questions...

yes ryan that answers my issues, I have been juggling between the 2 books I have trying to work out  how everything goes together thanks. bookmarked this for future reference, maybe you should sticky it to FAQ as this question came up alot in the old beginning rails forum?

Re: Another couple of Ruby basics questions...

I've been thinking about a sticky FAQ topic, I would need to discuss with the admins though. We'll see if that ever plays out.

Railscasts - Free Ruby on Rails Screencasts

Re: Another couple of Ruby basics questions...

That is definitely some good information there. It helps with the new 'mindset' one has to enter to start programming.

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.