Skip to content

Instantly share code, notes, and snippets.

View spanuska's full-sized avatar

Skylar Paul spanuska

View GitHub Profile
class ChangeDispenserCashregister
attr_accessor :total, :payment, :change_to_dispense
COINS = { quarter: 25, dime: 10, nickel: 5, penny: 1 }
def initialize
get_total
get_payment
class Interpolater
attr_accessor :data, :template
def initialize(template, data)
@template = template
@data = data
replace_bracketed_words
end
class Member
attr_accessor :username, :streak_dates
def initialize(username)
@username = username
@streak_dates = []
end
def practice
self.streak_dates << Date.today unless self.streak_dates.include?(Date.today)
class Streak
attr_accessor :username, :streak_dates
def initialize
@streak_dates = []
end
def extend_streak
self.streak_dates << Date.today unless self.streak_dates.include?(Date.today)
end
class Book
attr_accessor :title, :author
def initialize(args)
args = defaults.merge(args)
@title = args[:title]
...
end
def store_book
# logic for storing the book at a location on the shelf
class Shelf
...
def store(@book)
# logic to find the correct location on itself for @book
end
end
class Book
attr_accessor :title, :author
def initialize(args)
class Shelf
...
def store(book)
length = book.length
width = book.width
depth = book.depth
# logic to find the nearest spot that can accomodate the given dimensions
end
end
@spanuska
spanuska / book.rb
Last active March 29, 2016 23:38
Book with default arguments hash
class Book
....
def initialize(args)
args = defaults.merge(args) # If you're using Ruby <2.0, this is the way to go.
@title = args[:title]
@author = args[:author]
end
def defaults
{title: "Unknown", author: "Unknown"}
end
@spanuska
spanuska / book.rb
Last active March 30, 2016 13:27
Book with named keyword arguments
class Book
....
def initialize(title: 'Unknown', author: 'Unknown') # This is the Ruby >= 2.0 syntax.
@title = title
@author = author
end
end
@spanuska
spanuska / shelf.rb
Created March 31, 2016 13:28
Shelf with a dependency-managing wrapper method
class Shelf # without the wrapper
...
def store(book)
# imagine complicated logic here...
intermediate_result = book.storage_requirements / self.capacity
# imagine more complicated logic...
end
end
class Shelf # with wrapper