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
# Instructions: Write a program that tells you the following: | |
# Hours in a year. How many hours are in a year? | |
def hrs_per_yr | |
puts "Is this a leap year?" | |
leap = gets.chomp | |
x = 0 | |
while x == 0 do | |
if leap == "Yes" || leap == "yes" || leap == "Y" || leap == "y" | |
puts "There are #{366 * 24} hours in a leap year" |
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
# Full name greeting. Write a program that askes for a person's first name, then middle, and then last. Finally, it should greet the person using their full name | |
puts "Hello!\nWhat is your first name?" | |
first_name = gets.chomp | |
puts "Nice to meet you #{first_name}.\nWhat is your middle name?" | |
middle_name = gets.chomp | |
puts "#{middle_name}, huh? Interesting.\n What is your last name?" | |
last_name = gets.chomp | |
puts "Well I'm pleased to meet you #{first_name} #{middle_name} #{last_name}!" |
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
# "99 Bottles of Beer on the Wall." Write a program that prints out the lyrics to that beloved classic. | |
bottles = 99 | |
while bottles != 2 do | |
puts "#{bottles} bottles of beer on the wall! #{bottles} bottles of beer! Take one down! Pass it around! #{bottles - 1} bottles of beer on the wall!\n" | |
bottles -= 1 | |
end | |
puts "#{bottles} bottles of beer on the wall! #{bottles} bottles of beer! Take one down! Pass it around! #{bottles - 1} bottle of beer on the wall!\n" | |
bottles -= 1 | |
puts "#{bottles} bottle of beer on the wall! #{bottles} bottle of beer! Take it down! Pass it around! No more bottles of beer on the wall!" |
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
# Angry boss. Write an angry boss program that rudely asks you what you want. Whatever you answer, the angry boss should yell it back to you and then fire you. | |
puts "Huh? You again? What do you want!?" | |
want = gets.chomp.upcase | |
puts "WHAT?!!?!! YOU WANT \"#{want}\"!!??!! YOU'RE FIRED!!" | |
#Table of contents. Here's something for you to do in order to play around more with center, ljust, and rjust: write a program that will display a table of contents so that it looks like this: | |
page_width = 50 | |
puts ("\"The Book Of Mad Skilz\"".center(page_width)) |
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
# Building and sorting an array. Write the program we talked about at the beginning of this chapter, one that asks us to type as many words as we want (one word per line, continuing until we just press Enter on an empty line) and then repeats the words back to us in alphabetical order. Make sure to test your program thoroughly; for example, does hitting Enter on an empty line always exit your program? Even on the first line? And the second? Hint: There’s a lovely array method that will give you a sorted version of an array: sort. Use it! | |
puts "Type as many lines as you want and I'll alphabetize them for you:" | |
my_array = Array.new | |
new_word = "a" | |
while new_word != "" | |
new_word = gets.chomp | |
my_array.push(new_word) | |
end |
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
# Functions for preforming calculations | |
def addition(first_num, second_num) | |
puts "#{first_num} + #{second_num} = " | |
puts first_num.to_i + second_num.to_i | |
end | |
def subtraction(first_num, second_num) | |
puts "#{first_num} - #{second_num} = " | |
puts first_num.to_i - second_num.to_i |
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
class Planet | |
attr_reader :name, :color, :size, :number_of_moons, :rotation, :distance_from_the_sun | |
def initialize(title, hue, how_big) | |
@name = title | |
@color = hue | |
@size = how_big | |
@number_of_moons = rand(0..3) | |
@rotation = title.length/2 |
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
class Planet | |
attr_reader :name, :color, :size, :number_of_moons, :rotation, :distance_from_the_sun, :rain | |
def initialize(planet_hash) | |
@name = planet_hash[:name] | |
@color = planet_hash[:color] | |
@size = planet_hash[:size] | |
@number_of_moons = rand(0..3) | |
@rotation = @name.length/2 |
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
class SolarSystem | |
require "./PlanetClass" | |
attr_reader :name, :planets | |
def initialize(name) | |
@name = name | |
@planets = [] | |
end |
OlderNewer