Skip to content

Instantly share code, notes, and snippets.

require "minitest/autorun"
class Foo
@option = "default"
def self.build(value)
klass = Class.new self
klass.instance_variable_set :@option, value
klass
end
@xymbol
xymbol / foo_test.rb
Last active April 10, 2017 17:07
Inspired by "FP Concepts" talk by @martinosis at Ruby Montreal.
require "minitest/autorun"
module Foo
attr :add, :sub, :mul, :div
def initialize(*)
@add = -> (a, b) { a + b }.curry
@sub = -> (a, b) { a - b }.curry
@mul = -> (a, b) { a * b }.curry
@div = -> (a, b) { a / b }.curry
@xymbol
xymbol / open.rb
Last active December 21, 2015 14:44
require "open-uri"
options = {
content_length_proc: ->(length) {
if length && 0 < length
puts "content_length = #{length}"
end
},
progress_proc: ->(size) {
puts "progress = #{size}"
class Post < ActiveRecord::Base
validates_presence_of :title_en, :title_es
def localized_attribute_for(name, locale = I18n.locale)
"#{name}_#{locale}"
end
private
def method_missing(name, *args)
class Post < ActiveRecord::Base
validates_presence_of :locale
validates_inclusion_of :locale, in: %w(en es)
def self.with_locale(locale)
where locale: locale
end
end
class ApplicationController < ActionController::Base
array = [
{ x: 1, y: 2 },
{ x: 2, y: 2 },
{ x: 3, y: 2 }
]
array.sort_by do |k, v|
[k, v] # => [{:x=>1, :y=>2}, nil], [{:x=>2, :y=>2}, nil], [{:x=>3, :y=>2}, nil]
0
end
# This class provides most of the features you'd get from i18n-active_record,
# only simpler and lighter.
class Translation < ActiveRecord::Base
class Backend < I18n::Backend::KeyValue
module Implementation
attr_reader :store
def initialize(subtress = true)
@store = Store.new
super store, subtress
require "helper"
class CandidatesTest < MiniTest::Unit::TestCase
include FriendlyId::Test
class City < ActiveRecord::Base
extend FriendlyId
friendly_id :slug_candidates, use: :slugged
end
module ActiveModel
class RootSetting
attr_reader :klass, :value
def initialize(klass, value)
@klass = klass
@value = value
end
def root