Skip to content

Instantly share code, notes, and snippets.

@christopherslee
Last active December 20, 2015 15:19
Show Gist options
  • Save christopherslee/6153279 to your computer and use it in GitHub Desktop.
Save christopherslee/6153279 to your computer and use it in GitHub Desktop.
Intro to DSLs - Part 2 Answers
# lib/part_2/credit_score.rb
require_relative 'consumer'
module CreditScore
def self.simulate(&block)
consumer = Consumer.new
consumer.instance_eval(&block)
consumer.score
end
end
# lib/part_2/consumer.rb
class Consumer
attr_accessor :score
def initialize
@score = 0
end
# TODO Part 2
# Implement the DSL actions as methods to modify the consumer's score
def pay_bill(account, amount)
return @score += 1 if amount == 0
@score += (amount / 100.0).ceil
end
def awarded_credit(amount)
@score += (amount / 1000).ceil
end
def missed_payment(account, amount)
@score -= (amount.abs / 100.0).ceil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment