Skip to content

Instantly share code, notes, and snippets.

def getdue
puts "What is the amount due?"
due = gets.chomp
if !( /^\d*\.?\d{0,2}/.match(due) )
getdue
end
due.to_f
end
def getguess max_num
puts "Guess a number between 0 and #{max_num}: "
guess = gets.chomp
if !( /^[0-9]*/.match(guess) )
puts "Try again! NEED A NUMBER"
getguess (max_num)
end
return guess.to_i
end
#!/usr/bin/env ruby
def make_guess
#returns a guess that is one charater long OR compares against the whole word
print "Guess a single letter (a-z) or the entire word: "
guess = gets.chomp
if !(/[a-zA-z]/.match(guess))
puts "Please a enter a letter or word"
guess = make_guess
|----
|
|
|
|------\
|----
| 0
|
|
|------\
characters = {
"Tyrion Lannister" => "House Lannister",
"Jon Snow" => "Night's Watch",
"Hodor" => "House Stark",
"Stannis Baratheon" => "House Baratheon",
"Theon Greyjoy" => "House Greyjoy"
}
characters.each {|key, value| puts "#{key} represents the #{value}" }
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 {|movie| puts "#{movie[:year_released]}: #{movie[:title]}" }
@townie
townie / hangman_temp.rb
Created February 21, 2014 19:50
Hangman_template.rb
#TO DO LIST OF THING THAT NEED TO BE MADE
#1- count function
#2 - make it look pretty
#3- make it lose
def update_the_user_facing_word(hidden_word, guess, user_facing_word)
postion_in_the_word_hidden_word = 0
hidden_word.each_char do |letter|
#start loop
@townie
townie / syscheck1.rb
Created February 21, 2014 23:23
I love 1 liners
cuz_i_am_lazy= %w(75
100
85
65
84
87
95)
score= cuz_i_am_lazy.map{ |x| x.to_i}
puts score.inject(){|sum, value| sum + value} / score.length
@townie
townie / findunique.rb
Created February 24, 2014 00:57
find out the unique
def uniques(list)
list = list.split(',')
list.each_with_index do |num, index|
if list.count(num) > 1
list.delete_at(index)
end
end
list.join(',')
end
@townie
townie / rightmost_occurrence_test.rb
Created February 24, 2014 04:46
Fix for the path of the file to be tested
require 'minitest/spec'
require 'minitest/autorun'
require_relative '../lib/rightmost_occurrence'
describe "#rightmost_occurrence" do
it "returns nil if no matches are found" do
rightmost_occurrence('abc', 'x').must_equal nil
end