Skip to content

Instantly share code, notes, and snippets.

@alemohamad
Last active August 29, 2015 14:12
Show Gist options
  • Save alemohamad/14a67252742e77713a44 to your computer and use it in GitHub Desktop.
Save alemohamad/14a67252742e77713a44 to your computer and use it in GitHub Desktop.
Learning some Ruby
puts 365 * 24 # how many hours are in a year?
puts 10 * 365 * 24 * 60 # how many minutes are in a decade?
puts 30 * 365 * 24 * 60 * 60 # how many seconds old are you? (30 years)
puts 1031000000 / 60 / 60 / 24 / 365 # how many years do I have, if I'm 1031 million seconds old?
print "What's your first name?"
first_name = gets.chomp
print "What's your middle name?"
middle_name = gets.chomp
print "What's your last name?"
last_name = gets.chomp
puts "Hi there, #{first_name} #{middle_name} #{last_name}!"
print "What's your favorite number?"
fav_number = gets.chomp.to_i + 1
puts "Well, your new favorite number can be #{fav_number}, don't you think?"
puts "It's a better and enhanced number. :)"
puts "AND NOW WHAT DO YOU WANT!?!?"
answer = gets.chomp.upcase
puts "WHY WOULD YOU SAY THAT? \"#{answer}\"?!? YOU'RE FIRED!!"
lineWidth = 52
puts 'Table of Contents'.center lineWidth
puts
puts 'Chapter 1: Numbers'.ljust(lineWidth/2) + 'page 1'.rjust(lineWidth/2)
puts 'Chapter 2: Strings'.ljust(lineWidth/2) + 'page 72'.rjust(lineWidth/2)
puts 'Chapter 3: Variables'.ljust(lineWidth/2) + 'page 118'.rjust(lineWidth/2)
bottles = 99
while bottles > 1
puts "#{bottles} bottles of beer on the wall, #{bottles} bottles of beer."
bottles = bottles - 1 # removes 1 bottle
if bottles > 1
puts "Take one down, pass it around, #{bottles} bottles of beer on the wall..."
else
puts "Take one down, pass it around, 1 bottle of beer on the wall..."
end
puts ""
end
puts "If that one bottle should happen to fall, what a waste of alcohol!"
total_bye = 1
message = ''
while message != 'GOODBYE' or total_bye < 3
if !message.empty?
if message == message.upcase and message != 'GOODBYE'
# mostrar un año entre 1930 y 1950
year = 1930 + rand(20)
puts "NO, NOT SINCE #{year}!"
# resetear la cantidad de saludos
total_bye = 1
else
if(total_bye < 3)
puts "HUH?! TALK LOWDER, SON!"
if message == 'GOODBYE'
total_bye = total_bye + 1 # user has to say GOODBYE 3 times to exit the program
end
end
end
else
puts "HELLO SONNY, HOW ARE YOU?"
end
message = gets.chomp
end
puts "GOODBYE. I'LL MISS YOU!"
print "Start year:"
start_year = gets.chomp.to_i
print "End year:"
end_year = gets.chomp.to_i
if start_year >= end_year
puts "The start year must be earlier than the end year."
else
current_year = start_year
while current_year <= end_year
if current_year % 4 == 0
if current_year % 100 == 0
if current_year % 400 == 0
puts current_year
end
else
puts current_year
end
end
current_year = current_year + 1
end
end
puts "Start writing random words."
puts "When you want to stop, just press Enter without any content."
puts
words = []
message = gets.chomp
words.push message
while message.length > 0
message = gets.chomp
if !message.empty?
words.push message
end
end
puts words.sort
puts "Start writing random words."
puts "When you want to stop, just press Enter without any content."
puts
words = []
sorted_words = []
message = gets.chomp
words.push message
while message.length > 0
message = gets.chomp
if !message.empty?
words.push message
end
end
while !words.empty?
sorted_words.push words.delete(words.min)
end
puts sorted_words
array = [4, 1, 3, 2, 5, 6]
sorted = []
while !array.empty?
sorted.push array.delete(array.min)
end
puts sorted
lineWidth = 52
contents = []
contents.push ['Chapter 1: Numbers', 'page 1']
contents.push ['Chapter 2: Strings', 'page 72']
contents.push ['Chapter 3: Variables', 'page 118']
puts 'Table of Contents'.center lineWidth
puts
contents.each do |content|
puts content[0].ljust(lineWidth/2) + content[1].rjust(lineWidth/2)
end
now = Time.new
birthday = Time.mktime(1984, 2, 6, 12)
difference = now.to_i - birthday.to_i
puts "#{difference} seconds has passed since I was born"
million_secs = birthday + 1000000000
puts "At #{million_secs} I'll be a billion seconds old. :)"
puts "What is the year you were born?"
year = gets.chomp.to_i
puts "What is the month you were born?"
month = gets.chomp.to_i
puts "What is the day you were born?"
day = gets.chomp.to_i
birthday = Time.mktime(year, month, day)
now = Time.now
difference = now - birthday
years = difference.to_i / 60 / 60 / 24 / 365
puts "SPANK!\n" * years # user receives a spank for each birthday
class Dice
def initialize
roll
end
def roll
@numeroMostrado = 1 + rand(6)
end
def show
@numeroMostrado
end
def cheat num = 0
if num >= 1 and num <= 6
num
else
"If you're going to cheat, at least use a number between 1 and 6."
end
end
end
dice = Dice.new
puts dice.roll
puts
puts dice.show
puts
puts dice.cheat 5
class OrangeTree
def initialize
@totalOranges = 0
@height = 0
@age = 0
end
def height
@height
end
def passingOf365Days
@age = @age + 1
if @age > 100
puts "The orange tree has died. :("
exit
end
if @height == 0
@height = 1
else
@height = @height * 2
end
# when a year passes, oranges that were not collected, fall
@totalOranges = 0
# the tree should produce oranges from reaching 5 years of age
if @age >= 5
@totalOranges = @age
end
end
def countOranges
@totalOranges
end
def pickAnOrange
if @totalOranges > 0
@totalOranges = @totalOranges - 1
puts "That orange was delicious!"
else
puts "There are no more oranges to pick this year."
end
end
end
tree = OrangeTree.new
tree.passingOf365Days
tree.passingOf365Days
tree.passingOf365Days
puts tree.height
tree.passingOf365Days
puts tree.height
tree.passingOf365Days
puts tree.height
puts tree.countOranges
tree.pickAnOrange
tree.pickAnOrange
tree.pickAnOrange
tree.pickAnOrange
tree.pickAnOrange
tree.pickAnOrange
tree.pickAnOrange
tree.passingOf365Days
puts tree.countOranges
puts tree.height
def makeBong
hours = Time.now.hour
if hours > 12
hours - 12
end
hours.times do
yield
end
end
# this is a block that is being passed to a method
# to use its code as many times as the method wants to
makeBong do
puts 'BONG!'
end
# types of clients / percentages to apply
client_a = 4
client_b = 9
client_c = 12
# init variables
loan = 100
months_to_return = 3
selected_client = client_b
# end init
time_passed = months_to_return
total_payment = 0
percentage = (selected_client / 100.to_f) + 1.to_f
while time_passed > 0
if total_payment == 0
total_payment = loan * percentage
else
total_payment = total_payment * percentage
end
time_passed = time_passed - 1
end
puts "You have to pay a total of $#{total_payment.round(2)} to the bank."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment