Skip to content

Instantly share code, notes, and snippets.

@amfischer
Last active February 9, 2016 02:19
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 amfischer/368a50ea58160d2841f8 to your computer and use it in GitHub Desktop.
Save amfischer/368a50ea58160d2841f8 to your computer and use it in GitHub Desktop.
Ruby Practice
class Cat
attr_reader :color, :breed
attr_accessor :name
def initialize(color, breed)
@color = color
@breed = breed
@hungry = true
end
def feed(food)
puts "Mmmm, " + food + "!"
@hungry = false
end
def hungry?
if @hungry
puts "I\'m hungry!"
else
puts "I\'m full!"
end
@hungry
end
def speak
puts "Meow!"
end
end
kitty = Cat.new("grey", "Persian")
puts "Let\'s inspect our new cat:"
puts kitty.inspect
puts "What class does our new pet belong to?"
puts kitty.class
puts "Is our new cat an object?"
puts kitty.is_a?(Object)
puts "What color is our cat?"
puts kitty.color
puts "Let\'s give our new cat a name"
kitty.name = "Betsy"
puts kitty.name
puts "Is our cat hungry now?"
kitty.hungry?
puts "Let\'s feed our cat"
kitty.feed("tuna")
puts "Is our cat hungry now?"
kitty.hungry?
puts "Our cat can make noise"
kitty.speak
def fav_foods
food_array = [] # can also use array.new for new empty array
3.times do
puts "Name a favorite food"
food_array << gets.chomp
end
p food_array
food_array.each do |food| # do something to each element in food_array; that element is to be referred to as food
puts "I like #{food} too!" # the thing we are doing
end # ends the loop
puts "Your favorite foods are #{food_array.join(", ")}."
end
fav_foods
a = [1,2,3,4,5]
p a.map {|x| x*x} # map method
p a[1..4] # slice method
class Pet
attr_reader :color, :breed
attr_accessor :name
def initialize(color, breed)
@color = color
@breed = breed
@hungry = true
end
def feed(food)
puts "Mmmm, " + food + "!"
@hungry = false
end
def hungry?
if @hungry
puts "I\'m hungry!"
else
puts "I\'m full!"
end
@hungry
end
end
class Cat < Pet
def speak
puts "Meow!"
end
end
class Dog < Pet
def speak
puts "Woof!"
end
end
kitty = Cat.new("grey", "Persian")
puts "Let\'s inspect our new cat:"
puts kitty.inspect
puts "What class does our new pet belong to?"
puts kitty.class
puts "Is our new cat an object?"
puts kitty.is_a?(Object)
puts "What color is our cat?"
puts kitty.color
puts "Let\'s give our new cat a name"
kitty.name = "Betsy"
puts kitty.name
puts "Is our cat hungry now?"
kitty.hungry?
puts "Let\'s feed our cat"
kitty.feed("tuna")
puts "Is our cat hungry now?"
kitty.hungry?
puts "Our cat can make noise"
kitty.speak
puppy = Dog.new("golden", "Labrador")
puts "What color is our dog?"
puts puppy.color
puts "Let\'s give our dog a name"
puppy.name = "Coco"
puts puppy.name
puts "Our dog can make noise"
puts puppy.speak
puts puppy.breed
puts "hello"
p "hello"
def greeting
puts "Please enter your name"
name = gets.chomp
puts "Hello " + name
end
greeting
if (5+5==10)
puts "This is true"
else
puts "This is false"
end
def choose
puts "Do you like programming? Yes or no please."
choice = gets.chomp
case choice.downcase
when "yes"
puts "That\'s great!"
when "no"
puts "That\'s too bad!"
when "maybe"
puts "Glad you are giving it a chance!"
else
puts "I have no idea what that means"
end
end
choose
# number = 0
# while (number <= 10) do
# p "The number is now #{number}"
# number += 1
# end
(0..10).each do |n|
p "The new number is #{n}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment