Skip to content

Instantly share code, notes, and snippets.

@afomera
Created June 13, 2016 03:33
Show Gist options
  • Save afomera/dbbe9ac903c7838a4a076c12612bd580 to your computer and use it in GitHub Desktop.
Save afomera/dbbe9ac903c7838a4a076c12612bd580 to your computer and use it in GitHub Desktop.
Ruby Practice

Some Ruby Exercises

1.) Fizzbuzz ("Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.")

2.) Array manipulation (sort, plucking uniq values, checking existence of a value)

3.) Iterating over a Hashmap and an array

4.) Write a ruby script that takes a person's age in an integer and spits out their estimated birth date

5.) Write a ruby script that uses a nested conditional or case statement to output certain text based on input

# Given this array of Animals,
array = ['cow', 'sheep', 'sheep', 'pig', 'tiger', 'pig', 'lion', 'bear', 'cat']
# We can be sure we return only the unique values
animals = array.uniq
# Let's loop through and print them out
animals.each do |animal|
puts animal
end
# We want to get the first result in the array
puts animals[0] + " or " + animals.first
# We can last get the last value or others
puts animals.last
# We can sort our array
puts animals.sort
# We can also check to see if the array includes a value we want
# include? will return true or false
puts "the array has a cow" if animals.include? 'cow'
puts "the array does not have a fish" unless animals.include? 'fish'
require 'date'
puts "Hello, I'm going to guess a year timeframe in which you were born."
puts "Please enter your age in numbers. (Example: 32)"
age = gets.chomp.to_i
if age > 0
puts "You entered that you were #{age} years old"
else
puts "Please try entering your age again."
age = gets.chomp.to_i
end
# We know their age, so let's grab the current date
current_date = Date.today
# Find out how many months old they are
# We need to add 12 months to account for the year gap
# Because the user just enters an age, their bday could have been today for all we know
month_age = (age * 12) + 12
estimated_birth_date = current_date >> -month_age
puts "Your estimated birthdate is between #{estimated_birth_date} and #{estimated_birth_date.next_year}"
# Write a ruby script that uses a nested conditional or case statement to output certain text based on input
class Dog
attr_accessor :breed
attr_accessor :size
def initialize(breed, size)
@breed = breed
@size = size
end
end
puts "Hello, please enter information about your dog."
print "What breed is your dog (labrador, bulldog, poodle): "
breed = gets.chomp.to_s.downcase
print "What size is your dog? (small, medium, large): "
size = gets.chomp.to_s.downcase
dog = Dog.new(breed, size)
puts "------------------------"
case dog.breed
when "lab", "labrador"
puts "Labradors have a estimated lifespan of 10 to 12 years on average."
if dog.size == "small"
puts "Your #{dog.breed} is just a puppy, and has a lot of growing to do!"
elsif dog.size == "medium"
puts "Your #{dog.breed} will grow to be bigger still!"
elsif dog.size == "large"
puts "Your #{dog.breed} is an adult."
puts "Many adult female Labradors reach a weight of 55-75lbs and stand 21-22 inches high at the shoulder."
puts "Many adult male Labradors reach a weight of 65-85lbs and stand 22-23 inches at the shoulder"
else
puts "Sorry it appears you mistyped the size"
end
when "poodle"
puts "Poodles have an estimated life span of 12-15 years on average."
if dog.size == "small"
puts "Your #{dog.breed} is just a puppy, and has a lot of growing to do!"
puts "While poodles are one of the smaller dog breeds, they make fantastic companions."
elsif dog.size == "medium"
puts "Your #{dog.breed} is most likely under 5lbs."
elsif dog.size == "large"
puts "An average adult poodle can be anywhere between 5lbs and 8lbs."
else
puts "Sorry it appears you mistyped the size"
end
when "bulldog", "bull"
puts "Bulldogs have an estimated life span of 8 to 10 years on average."
if dog.size == "small"
puts "Your #{dog.breed} is just a puppy, and has a lot of growing to do!"
elsif dog.size == "medium"
puts "Your #{dog.breed} will grow to be bigger still!"
elsif dog.size == "large"
puts "Your #{dog.breed} is an adult."
else
puts "Sorry it appears you mistyped the size"
end
else
puts "I've never heard of that dog breed before!"
end
# Write a program that prints the numbers from 1 to 100.
# But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”.
# For numbers which are multiples of both three and five print “FizzBuzz”.
(1..100).each do |i|
if i % 3 == 0 && i % 5 == 0
puts "FizzBuzz"
elsif i % 3 == 0
puts "Fizz"
elsif i % 5 == 0
puts "Buzz"
else
puts i
end
end
# Because most of the community prefers each or map instead of for loops I went with the answer above
# But for is another way of doing it with basically the same code.
for i in 1..100
if i % 3 == 0 && i % 5 == 0
puts "FizzBuzz"
elsif i % 3 == 0
puts "Fizz"
elsif i % 5 == 0
puts "Buzz"
else
puts i
end
end
hashmap = { :face => "big", :shoulders => "wide", :legs => "tall" }
hashmap.each do |key,value|
puts key
puts value
end
# If you only wanted the key you could do
hashmap.each_key do |key|
puts key
end
# If you only wanted the values you could use
hashmap.each_value do |v|
puts v
end
# Checking to see if the key is there can be done with
hashmap.include?(:face)
hashmap.key?(:face)
# You can add a new key/value pair to the hash with
# You can also use this to update values
hashmap[:feet] = "wide"
hashmap[:face] = "skinny"
puts hashmap
# You can delete data from the hash by
hashmap.delete(:legs)
puts hashmap
@nynhex
Copy link

nynhex commented Jun 15, 2016

🎱 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment