This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> product = Product.create(cost: Money.new(500, "EUR")) | |
> product.cost | |
=> #<Money fractional:500 currency:EUR> | |
> product.cost.cents | |
=> 500 | |
> product.currency | |
=> "EUR" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Product < ActiveRecord::Base | |
def cost | |
Money.new(cents, currency) | |
end | |
def cost=(cost) | |
self.cents = cost.cents | |
self.currency = cost.currency.to_s | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Temperature | |
include Comparable | |
attr_reader :degrees | |
COLD = 20 | |
HOT = 25 | |
def initialize(degrees) | |
@degrees = degrees | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Grade | |
include Comparable | |
attr_reader :percentage | |
def initialize(percentage) | |
@percentage = percentage | |
end | |
def better_than?(other) | |
self > other |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> gary = Person.create(name: "Gary") | |
> gary.address_city = "Brooklyn" | |
> gary.address_state = "NY" | |
> gary.address | |
=> #<Address:0x007fcbfcce0188 @city="Brooklyn", @state="NY"> | |
> gary.address = Address.new("Brooklyn", "NY") | |
> gary.address | |
=> #<Address:0x007fcbfa3b2e78 @city="Brooklyn", @state="NY"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Address | |
attr_reader :city, :state | |
def initialize(city, state) | |
@city, @state = city, state | |
end | |
def ==(other_address) | |
city == other_address.city && state == other_address.state | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person < ActiveRecord::Base | |
def address | |
Address.new(address_city, address_state) | |
end | |
def address=(address) | |
self.address_city = address.city | |
self.address_state = address.state | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Event < ActiveRecord::Base | |
def date_range | |
DateRange.new(start_date, end_date) | |
end | |
def date_range=(date_range) | |
self.start_date = date_range.start_date | |
self.end_date = date_range.end_date | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DateRange | |
attr_reader :start_date, :end_date | |
def initialize(start_date, end_date) | |
@start_date, @end_date = start_date, end_date | |
end | |
def include_date?(date) | |
date >= start_date && date <= end_date | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rectangle { | |
constructor(height, width, color) { | |
this.height = height | |
this.width = width | |
this.color = color | |
} | |
draw(layer, stage) { | |
const rectangle = new Konva.Rect({ | |
x: Math.random() * stage.getWidth(), |