Skip to content

Instantly share code, notes, and snippets.

View practicingruby's full-sized avatar

Gregory Brown practicingruby

View GitHub Profile
class Array
def group_by
hash = Hash.new { |h,k| h[k] = [] }
each { |i| hash[yield(i)] << i }
hash
end
end
def distance_to_hotel(day, index)
case
when index >= @grid.size - 1
''
when day[:lodgings].empty?
UNKNOWN_DISTANCE
when overnight_transport?(day)
OVERNIGHT_TRANSPORT_LABEL
when day[:arrive]
day[:arrive].distance_from(hotel_location(day)
def path_to_hotel?(day)
car_rented?(day) || hotel_near_destination?(day)
end
def hello
"Hello World"
end
module Measurable
def area
width * height
end
attr_reader :width, :height
end
class Rectangle
include Measurable
require "rubygems"
require "sinatra"
get "/next" do
tell_itunes "next track"
end
get "/previous" do
tell_itunes "previous track"
end
def reduce(initial_value, &block)
reduce_fun = fun(element, acc) { block(acc, element) }
erl.lists.foldl(reduce_fun, initial_value, self)
end
afsasf
class Contact
def initialize(&block)
instance_eval &block
end
def name(new_name=nil)
@name = new_name if new_name
@name
end
## Extract Class (http://refactoring.com/catalog/extractClass.html)
class Person
attr_accessor :name, :office_area_code, :office_number
def telephone_number
[office_area_code, office_number].join('-')
end
end