Skip to content

Instantly share code, notes, and snippets.

@BrianJoyce
BrianJoyce / debug_arg_1.rb
Created October 2, 2012 17:56
debugging_argument_1
def mean(*numbers)
sum = numbers.inject(:+)
return sum / numbers.length
end
# This will throw an error. Change this line so that it works.
sample_avg = mean(5, 3, 6, 10)
@BrianJoyce
BrianJoyce / gist:3825744
Created October 3, 2012 08:10
Birthdate stuff ... pet hate
class Birthday
def initalize
@days = 0
@months = 0
end
def days_in_year(year)
leap_year?(year) ? 366 : 365
end
@BrianJoyce
BrianJoyce / gist:3844085
Created October 6, 2012 05:40
Arne roman numerals part 2
class RomanNumerals
def initialize
@reference = {"1"=> "I", "5"=> "V", "10" => "X", "50"=> "L", "100" => "C", "500"=> "D", "1000"=> "M",
"4"=> "IV", "9"=>"IX","40" => "XL", "90" => "XC", "400"=> "CD", "900" => "CM"}
end
def to_roman(number,long_string = '')
@reference.keys.map{|i| i.to_i}.sort.reverse.each do |i|
count,number = difference(number,i.to_i)
@BrianJoyce
BrianJoyce / dirtrygrandma.rb
Created October 6, 2012 06:23
Dirty grandma
# Solution for Challenge: Deaf Grandma. Started 2012-10-01T22:56:30+00:00
class DeafGrandma
def echo_grandma
while (a = gets.chomp) != ""
(a == a.downcase) ? (puts "HUH?! SPEAK UP, SONNY!") : (puts "NO, NOT SINCE 1938!")
end
end
end
@BrianJoyce
BrianJoyce / new_student.rb
Created October 6, 2012 06:46
class example
class Student
def height(height)
@height = height
end
def hair_color(hair)
@hair_color = hair
end
def shoe_size(dog)
# Put your answers here!
@BrianJoyce
BrianJoyce / bowling_game.rb
Created October 8, 2012 18:49
bowling game
require 'pry'
class BowlingGame
attr_accessor :max_pins_available, :roll_score, :frame_scores
def initialize
@game = []
@frame_scores = []
end
@BrianJoyce
BrianJoyce / bowlingGame.rb
Created October 10, 2012 06:04
Bowling Game DBC
require 'pry'
class BowlingGame
binding.pry
attr_accessor :total, :game
def initialize
@game = []
@total = 0
9.times {|f| @game << [0,0]}
9.times {|f| @game[f] = roll_frame(f)}
@BrianJoyce
BrianJoyce / regex.rb
Created October 10, 2012 07:56
regex
require 'pry'
# Determine whether a string contains a Social Security number.
def has_ssn?(string)
string.match(/\d{3}.\d{2}.\d{4}/) ? true : false
end
# Return the Social Security number from a string.
def grab_ssn(string)
string.slice(/\d{3}.\d{2}.\d{4}/) # ? true : false
@BrianJoyce
BrianJoyce / bin_search.rb
Created October 12, 2012 00:23
Binary_search
def binary_search(search,array)
# binding.pry
array.sort!
@h_i ||= array.size
@m_i ||= array.size / 2
@l_i ||= 0
return @m_i if search == array[@m_i]
return -1 if @h_i - @m_i == 1
if search > array[@m_i] # top half