This file contains hidden or 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
module Prunable | |
def from_assets(assets) | |
assets.select &method(:is_prunable?) | |
end | |
def is_prunable?(asset) | |
asset.created_at < Date.today - 30 | |
end | |
module_function from_assets, is_prunable? |
This file contains hidden or 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
require_relative '../foo' | |
describe StatefulCalculator do | |
before(:all) do | |
@subject = StatefulCalculator.new | |
end | |
it 'should add some numbers' do | |
expect(@subject.add(5).add(3).add(10).result).to eq(18) | |
end |
This file contains hidden or 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 StatefulCalculator | |
def initialize(total) | |
@total = total | |
end | |
def add(x) | |
StatefulCalculator.new(@total + x) | |
end | |
def sub(x) |
This file contains hidden or 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
# implemented in the controller action, result from the | |
# calculator operation is passed to the Repository | |
# app/controllers/calculation.rb | |
def create | |
h = ActiveSupport::JSON.decode(request.body) | |
fx = lambda do |op, mod, repo| | |
lambda do |x, y| | |
result = mod.method(op.to_sym).to_proc[x, y] |
This file contains hidden or 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
# a fake repository | |
class Repository | |
def self.save_calculation(op, n1, n2, result) | |
puts "just saved #{op}, #{n1}, #{n2}, #{result}" | |
end | |
end |
This file contains hidden or 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 StatefulCalculator | |
def initialize | |
@total = 0 | |
end | |
def add(x) | |
@total += x | |
self | |
end | |
This file contains hidden or 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 StatefulCalculator | |
def initialize | |
@total = 0 | |
end | |
def add(x) | |
@total += x | |
self | |
end | |
This file contains hidden or 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 StatefulCalculator | |
def initialize | |
@total = 0 | |
end | |
def add(x) | |
@total += x | |
self | |
end |
This file contains hidden or 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
a = [["a", 1], ["b", 2]] | |
h = a.inject Hash.new(9999) do |hash, pair| | |
hash.merge Hash[[pair]] | |
end | |
h["a"] # 1 | |
h["foo"] # 9999 |
This file contains hidden or 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
function sync(gen) { | |
var iterable, resume, check, vals, ops; | |
vals = []; | |
ops = 0; | |
check = function() { | |
if (vals.length == ops) { | |
if (ops == 1) { | |
iterable.next(vals[0]); |