Created
May 30, 2013 23:05
-
-
Save ashleygwilliams/5681970 to your computer and use it in GitHub Desktop.
go through this program and explain what the compiler does line by line. give line numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# instance methods - | |
# def methodName | |
# end | |
# * only belong to objects | |
# * access them via dot notation | |
# * can use instance variables | |
# | |
# instance variables - | |
# def initialize | |
# @variable = value | |
# end | |
# * only belong to objects | |
# * use attr_accessor :variable to access via dot notation | |
# * can be used by instance methods | |
# | |
# class methods | |
# class variables | |
#DEFINING CLASS | |
class Animal | |
attr_accessor :name, :sound, :numlegs | |
def initialize(name, sound) | |
@name = name | |
@sound = sound | |
@numlegs = 4 | |
end | |
def sayHello | |
puts "hello, my name is " + @name + " and i have " + @numlegs.to_s + " legs" | |
end | |
end | |
def makeAnimalSayHello(animalObject) | |
animalObject.sayHello | |
end | |
# MAKE OBJECTS | |
horse = Animal.new("Bob", "neigh") | |
dog = Animal.new("Sally", "woof") | |
cat = Animal.new("Marvin", "meow") | |
fish = Animal.new("Felipe", "nothing") | |
animals = [horse, dog, cat, fish] | |
animals.each do |animal| | |
makeAnimalSayHello(animal) | |
end | |
makeAnimalSayHello(horse) | |
makeAnimalSayHello(dog) | |
makeAnimalSayHello(cat) | |
makeAnimalSayHello(fish) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. line 39: | |
step1: process [Animal.new("Bob", "neigh")] | |
a. [Animal]first we go to class Animal[line 20] | |
b. [.new]find the initialize method[line 23] | |
c. [("Bob", "neigh")] set parameter values. [line23] | |
- name = "Bob" | |
- sound = "neigh" | |
d.run initialize method[line24-26] with parameters we just set | |
- store name paramter="Bob" in an instance variable called @name[line24] | |
- store sound parameter="neigh" in an instance variable called @sound[line25] | |
- store constant 4 in an instance variable called @numlegs[line26] | |
-DONE [line 27] | |
2. Line 40 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment