Last active
August 29, 2015 14:27
-
-
Save catob/a4793b37c5cf8d24138d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Pet | |
attr_reader :color, :breed | |
attr_accessor :name | |
def initialize(color, breed) | |
@color = color | |
@breed = breed | |
@hungry = true | |
end | |
def feed(food) | |
puts "Mmm, " + food + "!" | |
@hungry = false | |
end | |
def hungry? | |
if @hungry | |
puts "I'm hungry!" | |
else | |
puts "I'm full!" | |
end | |
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") | |
dog = Dog.new("Black", "Boston Terrier") | |
puts "Let's inspect our new cat:" | |
puts kitty.inspect | |
puts "What class does the cat belong to?" | |
puts kitty.class | |
puts "Is our new kitty an object?" | |
puts kitty.is_a?(Object) | |
puts "What color is our cat?" | |
puts kitty.color | |
puts "Let's give our cat a name" | |
kitty.name = "Charlie" | |
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 "Can our cat make a noise?" | |
kitty.speak | |
puts "Let's check out our new dog!" | |
dog.speak | |
puts "What kind of dog is it?" | |
puts dog.breed | |
puts "Let's se if it's hungry" | |
dog.hungry? | |
puts "Let's feed it" | |
dog.feed("Bacon") |
This file contains hidden or 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
def fav_foods | |
food_array = [] | |
3.times do | |
puts "What's your favorite food?" | |
food_array << gets.chomp | |
end | |
p food_array | |
puts "Your favorite foods are #{food_array.join(", ")}" | |
food_array.each do |food| | |
puts "I like #{food} too!" | |
end | |
end | |
fav_foods |
This file contains hidden or 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
def greeting | |
puts "What is your name?" | |
user_input = gets.chomp | |
puts "Hello #{user_input}" | |
end | |
greeting |
This file contains hidden or 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
if (5+5==10) | |
puts "this is true" | |
else | |
puts "this is false" | |
end |
This file contains hidden or 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
def choose | |
puts "Do you like programming? Yes, no, or maybe 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're giving it a chance!" | |
else | |
puts "I have no idea what that means." | |
end | |
end | |
choose |
This file contains hidden or 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
# 3.times do | |
# puts "Hello" | |
# end | |
# number = 0 | |
# while (number <= 10) do | |
# p "the number is now #{number}" | |
# number += 1 | |
# end | |
# (1..10).each do |n| | |
# puts "The number is #{n}" | |
# end | |
# def choose | |
# count = 0 | |
# while count.to_i < 1 | |
# puts "Do you like programming? Yes, no, or maybe please" | |
# choice = gets.chomp | |
# case choice.downcase | |
# when "yes" | |
# puts "That's great!" | |
# count += 1 | |
# when "no" | |
# puts "That's too bad!" | |
# count += 1 | |
# when "maybe" | |
# puts "Glad you're giving it a chance!" | |
# count += 1 | |
# else | |
# puts "I have no idea what that means." | |
# end | |
# end | |
# end | |
# choose | |
def choose | |
while true do | |
puts "Do you like programming? Yes, no, or maybe please" | |
choice = gets.chomp | |
if choice == "yes" | |
puts "That's great!" | |
break | |
elsif choice == "no" | |
puts "That's too bad!" | |
break | |
elsif choice == "maybe" | |
puts "Glad you're giving it a chance!" | |
break | |
else | |
puts "I have no idea what that means." | |
end | |
end | |
end | |
choose |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment