add extra files
This commit is contained in:
60
ruby/get_number.rb
Normal file
60
ruby/get_number.rb
Normal file
@@ -0,0 +1,60 @@
|
||||
# Get My Number Game
|
||||
# written by: you!
|
||||
|
||||
puts "Welcome to 'Get My Number!'"
|
||||
|
||||
print "What's your name? "
|
||||
|
||||
input = gets
|
||||
|
||||
name = input.chomp
|
||||
|
||||
puts "Welcome, #{name}!"
|
||||
|
||||
# Store a random number for the player to guess.
|
||||
|
||||
puts "I've got a number between 1 and 100."
|
||||
|
||||
puts "Can you guess it?"
|
||||
|
||||
target = rand(100) + 1
|
||||
|
||||
# Track how many gueses the player has made.
|
||||
|
||||
num_guesses = 0
|
||||
|
||||
# Track whether the player has guessed correctly
|
||||
|
||||
guessed_it = false
|
||||
|
||||
until num_guesses == 10 || guessed_it
|
||||
|
||||
puts "You've got #{10 - num_guesses} guesses left."
|
||||
|
||||
print "Make a guess."
|
||||
|
||||
guess = gets.to_i
|
||||
|
||||
num_guesses += 1
|
||||
|
||||
# Compare the guess to the target.
|
||||
# Print the appropriate message.
|
||||
|
||||
if guess < target
|
||||
puts "Oops. Your guess was LOW."
|
||||
elsif guess > target
|
||||
puts "Oops. Your guess was HIGH."
|
||||
elsif guess == target
|
||||
puts "Good job, #{name}!"
|
||||
puts "You guessed my number in #{num_guesses} gueses!"
|
||||
guessed_it = true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# If player ran out of turns, tell them what the nubmer was.
|
||||
|
||||
unless guessed_it
|
||||
puts "Sorry. You didn't get my number. (It was #{target}.)"
|
||||
end
|
||||
|
||||
13
ruby/soda_methods.rb
Normal file
13
ruby/soda_methods.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
def order_soda(flavor, size = "medium", quantity = 1)
|
||||
if quantity == 1
|
||||
plural = "soda"
|
||||
else
|
||||
plural = "sodas"
|
||||
end
|
||||
puts "#{quantity} #{size} #{flavor} #{plural}!"
|
||||
end
|
||||
|
||||
|
||||
order_soda("orange")
|
||||
order_soda("lemon-lime", "small", 2)
|
||||
order_soda("grape", "large")
|
||||
3
ruby/sort.rb
Normal file
3
ruby/sort.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
numbers = [5, 3, 2, 4, 1]
|
||||
|
||||
print numbers.sort
|
||||
29
ruby/vehicle_methods.rb
Normal file
29
ruby/vehicle_methods.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
def accelerate
|
||||
puts "Stepping on the gas"
|
||||
puts "Speeding up"
|
||||
end
|
||||
|
||||
def sound_horn
|
||||
puts "Pressing the horn button"
|
||||
puts "Beep beep!"
|
||||
end
|
||||
|
||||
def use_headlights(brightness)
|
||||
puts "Turning on #{brightness} headlights"
|
||||
puts "Watch out for deer!"
|
||||
end
|
||||
|
||||
def use_headlights(brightness = "low-beam")
|
||||
puts "Turning on #{brightness} headlights"
|
||||
puts "Watch out for deer!"
|
||||
end
|
||||
|
||||
def mileage(miles_driven, gas_used)
|
||||
return miles_driven / gas_used
|
||||
end
|
||||
|
||||
trip_mileage = mileage(400, 12)
|
||||
puts "You got #{trip_mileage} MPG on this trip."
|
||||
|
||||
lifetime_mileage = mileage(11432, 366)
|
||||
puts "This car averages #{lifetime_mileage} MPG."
|
||||
Reference in New Issue
Block a user