Skip to content

Instantly share code, notes, and snippets.

@georgekettle
Created January 13, 2021 00:53
Show Gist options
  • Save georgekettle/339770830181fe663619d5c31addc06d to your computer and use it in GitHub Desktop.
Save georgekettle/339770830181fe663619d5c31addc06d to your computer and use it in GitHub Desktop.
ruby_basics_livecode
require 'date'
# Livecode
def days_until_christmas
# return the number of days until net christmas
today = Date.today
current_year = Date.today.year
christmas = Date.new(current_year, 12, 25)
# subtract to get the amount of days b/w
christmas = christmas.next_year if today > christmas
return (christmas - today).to_i
end
# Advanced version
def days_until_xmas(someday = Date.today)
xmas_date = Date.new(someday.year, 12, 25)
xmas_date = xmas_date.next_year if xmas_date < someday
return (xmas_date - someday).to_i
end
# We want to display "true" to test our method (TDD - Test Driven Development)
# Just like with our 'rake' command, we get green results when our tests' pass, this is like
puts days_until_xmas == 266 #change this number by the number of days until xmas
puts days_until_xmas(Date.new(2018,12,25)) == 0
puts days_until_xmas(Date.new(2018,12,26)) == 364
puts "----------------------------------------"
puts "-----Welcome to The Price is Right!-----"
puts "----------------------------------------"
puts "Guess the price of this wonderful imaginary product"
# specify a range
max = 100
min = 1
# specify random number
price = rand(min..max)
# specify count & guess
puts "What is your guess today?"
guess = gets.chomp.to_i
count = 1
# guess a number
# check if its wrong
# if wrong -> add to the count of guesses
# and make them guess again
# if its correct -> exit the loop
# loop
while guess != price
if guess > price
p "Your guess is too high"
else
p "Your guess is too low"
end
puts "Wrong guess, try again:"
guess = gets.chomp.to_i
count += 1
end
puts "******************************"
puts "-------Congratulations!-------"
puts "******************************"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment