Skip to content

Instantly share code, notes, and snippets.

/rb1

Created July 10, 2016 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d47d175eb2b23f852b0549bc95d574f7 to your computer and use it in GitHub Desktop.
Save anonymous/d47d175eb2b23f852b0549bc95d574f7 to your computer and use it in GitHub Desktop.
variables are parameters
#doggy_pal.rb
#I usually set up 2 Dog instances
class Dog
#instance variables
attr_accessor :name, :breed
#instance method
def gets_along_with(buddy)
puts "#{@name} is friendly with #{buddy.name}."
puts "#{@name} is a #{@breed}.\n#{buddy.name} is a #{breed}."
#Notice it works without instance variable set equal to parameters.
end
end
=Begin
Alpha = Dog.new
Alpha.name = "Adam"
Alpha.breed = "Alpha"
Collie = Dog.new
Collie.name = "Chris"
Collie.breed = "Collie"
Alpha.gets_along_with(Collie)
=End
Raw doggy_pal2.rb
#doggy_pal2.rb
#I usually set up 1 instance
class Animal
attr_accessor :name, :what_animal, :breed
def report(personality)
puts "#{@name} is a #{@what_animal}."
puts "#{@name} has a #{personality.breed} personality."
@breed = personality #so why am I setting my variable = parameter?
#My thesis is that I do this when I deal with only one instance...
#am I right?
end
end
=Begin
Dog = Animal.new
Dog.name = "Adam"
Dog.what_animal = "Dog"
Dog.breed = "Collie"
Dog.report("Collie")
this input works
If I were to set up 2 instances
Dog = Animal.new
Dog.name = "Adam"
Dog.what_animal = "Dog"
Dog.breed = "Collie"
Cat = Animal.new
Cat.name = "Rocky"
Cat.what_animal = "Cat"
Cat.breed = "tabby"
Dog.report(Cat)
this input does not work
=End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment