Skip to content

Instantly share code, notes, and snippets.

View radavis's full-sized avatar

Richard Davis radavis

View GitHub Profile
@radavis
radavis / guess.rb
Last active December 21, 2015 05:09
MAX = 10
answer = rand(MAX)
while true do
print "Guess a number between 0 and #{MAX}: "
guess = gets.chomp.to_i
if guess > answer
puts "\nToo high, try again..."
@radavis
radavis / gist:6281073
Created August 20, 2013 13:04
Blackjack project. My partner was Carl.
#!/usr/bin/env ruby
# encoding: UTF-8
SUITS = ['♠', '♣', '♥', '♦']
VALUES = ( 2..10 ).to_a.map{ |n| n.to_s }.concat( ['J', 'Q', 'K', 'A'] )
# build_deck
# returns a shuffled array of 52 cards
def build_deck
deck =[]
@radavis
radavis / gist:6301193
Created August 21, 2013 22:45
Fun times with FizzBuzz
(1..100).each do |i|
print 'Fizz' if i % 3 == 0
print 'Buzz'if i % 5 == 0
print i if i % 3 != 0 and i % 5 != 0
puts
end
@radavis
radavis / gist:6306735
Created August 22, 2013 12:51
Min, Max and Average
scores = [75, 100, 85, 65, 84, 87, 95]
p scores
puts "Average: #{scores.inject(0){|sum, i| sum + i} / scores.length.to_f}"
puts "Minimum: #{scores.min}"
puts "Maximum: #{scores.max}"
@radavis
radavis / gist:6309326
Created August 22, 2013 16:10
Game of Thrones madness
characters = {
"Tyrion Lannister" => "House Lannister",
"Jon Snow" => "Night's Watch",
"Hodor" => "House Stark",
"Stannis Baratheon" => "House Baratheon",
"Theon Greyjoy" => "House Greyjoy"
}
characters.keys.each do |name|
puts "#{name} represents the #{characters[name]}"
@radavis
radavis / gist:6309495
Created August 22, 2013 16:24
Movie madness
favorite_movies = [
{ title: 'The Big Lebowski', year_released: 1998, director: 'Joel Coen', imdb_rating: 8.2 },
{ title: 'The Shining', year_released: 1980, director: 'Stanley Kubrick', imdb_rating: 8.5 },
{ title: 'Troll 2', year_released: 1990, directory: 'Claudio Fragasso', imdb_rating: 2.5 }
]
favorite_movies.each do |movie|
puts "#{movie[:year_released]}: #{movie[:title]}"
end
@radavis
radavis / next_train.rb
Created August 23, 2013 17:36
The Next Train assignment.
EASTEREGG = "journey.txt"
class TrainSchedule
FILENAME = 'schedule.txt'
@@schedule = {}
def initialize
if @@schedule.empty?
File.open(FILENAME, 'r').each do |line|
train, time = line.split(",")
@radavis
radavis / deep_copy.rb
Created August 26, 2013 12:23
Make a copy of an object in ruby
def deep_copy(obj)
# from: http://stackoverflow.com/a/8206537
# serializes the object, then unserializes it to make a duplicate
return Marshal.load(Marshal.dump(obj))
end
class Television
attr_accessor :manufacturer, :hd_compliant, :aspect_ratio, :screen_size
def initialize(manufacturer, hd_compliant, aspect_ratio, screen_size)
@manufacturer = manufacturer
@hd_compliant = hd_compliant
@aspect_ratio = aspect_ratio
@screen_size = screen_size
end
class Car
attr_reader :color, :owner, :cylinders
def initialize(color, owner, cylinders)
@color = color
@owner = owner
@cylinders = cylinders
end
end