Skip to content

Instantly share code, notes, and snippets.

@JoseJRVazquez
Created April 10, 2014 18:15
Show Gist options
  • Save JoseJRVazquez/10408550 to your computer and use it in GitHub Desktop.
Save JoseJRVazquez/10408550 to your computer and use it in GitHub Desktop.
Now I know what classes are in CSS and HTML. But now for ruby....ouch
So, according to the lesson, Ruby is an Object-Oriented language. In an OO language, objects communicate with each other via messages. Thats to say, messages containing instructions are sent to objects, then something happens. Here's an example they give:
numbers#This is a variable = [1,2,3] #This is the Object
numbers.length #Length is a method with a very explicit set of instructions, which for this is to query the object for its length
#=> 3 #the answer the object returns from the method that asked the question in the first place (its a nosey bitch, MYOB)
SO in this, its declaring a variable named NUMBERS and setting it equal to an array consisting of 1, 2 and 3. Our numbers array is in fact, an object. On the next line, we are sending our numbers object a message. The message is basically asking the object for its length. As you know, length is also a method, but in the abstract it's really just a message with an explicit set of instructions. After the numbers object receives the message, which is basically "hey array, give me your length and be quick about it", the numbers object returns with an answer -- 3.
SO where do objects come from...good question
This is where we get the concept of CLASSES. IN the example, the numbers object, came from the ARRAY class. Classes serve as a blueprintfrom which you can make an OBJECT in the first place. The example it gives is:
numbers = [1,2,3]
names = ["Steve", "Bill"]
These are two different objects, but both use the same bluleprint which is an ARRAY. While they are difference, they share the same abilities, such as responding to a .length command.
numbers = [1,2,3] #object
numbers.length #method
#=> 3 #result
names = ["Steve", "Bill"]
names.length
#=> 2
This leads the lesson into CLASS INSTANCES
Apparently, you can call both numbers and names above objects. But they are also an instrance of the ARRAY class. For this lesson, they are focusing on instances rather than objects. The exmaple they are using is to define a class called Person. Then they assign an instance of it using steve_jobs.
Class Person #Definition of the class
end #ends the Definition
steve_jobs #the unique instance = Person.new #The Instance created
Note: Class names always start with a capital letter and use camel-case.
According to the lesson: An instance is a unique state for a class. For example, steve_jobs is a type of Person, and steve_jobs is therefore unique. There are other people (i.e. instances of Person) that could be created from the Person class, but there's only one steve_jobs.
However, you can have another steve jobs instance, but it will be indentified as a unique instance of that name. After all, there are many john smiths in the world.
2.0.0-p451 :001 > class Person
2.0.0-p451 :002?> end
=> nil
2.0.0-p451 :003 > steve_jobs = Person.new
=> #<Person:0x0000010186ccf0>
2.0.0-p451 :004 > steve_jobs = Person.new
=> #<Person:0x0000010183e1c0>
2.0.0-p451 :005 > p steve_jobs
#<Person:0x0000010183e1c0>
=> #<Person:0x0000010183e1c0>
2.0.0-p451 :006 >
Note how the object ID (#<Person:0x....) is different each time your print steve_jobs. That's because each instance of the Person class in unique according to the lesson.
Of course, you knew they would chain concepts together. In this one, we are create INSTANCE METHODS
So when we have an instance of the Person class, we can call in INSTANCE METHODS on it. The example they give is the methods EAT and SLEEP. So they create the methods as follows:
class Person
def eat #The method attached to the class
"yumyum" #the instruction
end
end
steve_jobs = Person.new
steve_jobs.eat #the chained instance method has it call out the correct instructions
#=> "yumyum"
bill_gates = Person.new
bill_gates.eat
#=> "yumyum"
Moving on the Attributes and Instance Variables
An instace of the Person class can EAT using an INSTANCE METHOD, but can have attirbutes to it as well. The example given is the instance of the Person class might have a name. The name would be an attribute of Person Instance:
SETTER METHOD = Purpose is to set and attribute. For this example its stored using an INSTRANCE VARIBALE. The name its given
here is @full_name, and it was set to the full_name argument being passed to the name= method.
In plain English you could say that "The name method sets an instance variable called full name."
Here they start using an equals sign = to set the method, common for setter methods. Without setting it this way, it
couldnt be used by anthing outside the method it was declared in.
Next, the second part of the formula is a GETTER METHOD. its only purpose is to retrive the attribute. It returns the @full_name instance variable. BEcause its declared, we can reference it again in the class.
Instance varibles always start with an @, so @full_name is the instance variable
class Person #declares the person CLASS
def name=(full_name) #defined method of NAME= AKA a Setter Method.
@full_name #this is the instance variable= full_name
end
def name
@full_name
end
end
apple_ceo = Person.new
apple_ceo.name=("Tim Cook")
p "My name is #{apple_ceo.name}"
#=> "My name is Tim Cook"
google_ceo = Person.new
google_ceo.name=("Larry Page")
p "My name is #{google_ceo.name}"
#=> "My name is Larry Page"
Heres what happened when I ran it:
.0-p451 :014 > class Person
2.0.0-p451 :015?> def name=(full_name)
2.0.0-p451 :016?> @full_name = full_name
2.0.0-p451 :017?> end
2.0.0-p451 :018?>
2.0.0-p451 :019 > def name
2.0.0-p451 :020?> @full_name
2.0.0-p451 :021?> end
2.0.0-p451 :022?> end
=> nil
2.0.0-p451 :023 > apple_seo = Person.new
=> #<Person:0x0000010199a8e8>
2.0.0-p451 :024 > apple_ceo.name=("Tim Cook")
NameError: undefined local variable or method `apple_ceo' for main:Object
from (irb):24
from /usr/local/rvm/rubies/ruby-2.0.0-p451/bin/irb:12:in `<main>'
2.0.0-p451 :025 > apple_seo.name=("Tim Cook")
=> "Tim Cook"
2.0.0-p451 :026 > p "My name is #{apple_seo.name}
2.0.0-p451 :027"> p "My name is #{apple_seo.name}"
SyntaxError: (irb):27: syntax error, unexpected tCONSTANT, expecting end-of-input
p "My name is #{apple_seo.name}"
^
from /usr/local/rvm/rubies/ruby-2.0.0-p451/bin/irb:12:in `<main>'
2.0.0-p451 :028 > p "My name is #{apple_seo.name}"
"My name is Tim Cook"
=> "My name is Tim Cook"
2.0.0-p451 :029 > google_ceo = Person.new
=> #<Person:0x00000101922bb8>
2.0.0-p451 :030 > google_ceo.name=("Larry Page")
=> "Larry Page"
2.0.0-p451 :031 > p "My name is #{google_ceo.name}"
"My name is Larry Page"
=> "My name is Larry Page"
2.0.0-p451 :032 >
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment