Skip to content

Instantly share code, notes, and snippets.

@samsm
samsm / comparable.rb
Last active November 29, 2023 17:17
Ruby Comparable and Range!
# I was familiar with this, the keys here are "include Comparable" and the <=> method.
# <=> return 1 for larger, 0 for equal, -1 for smaller than "other"
class UndergradClassification
include Comparable
CLASSIFICATIONS = ["freshman", "sophomore", "junior", "senior"]
attr_reader :name, :index
def initialize(name)
@samsm
samsm / yield.rb
Last active March 31, 2019 22:36
Couple examples of yield
# yield is like "do the thing that is in the block"
def do_twice
yield
puts "again!"
yield
end
do_twice { puts "hello world" }
do_twice do
@samsm
samsm / something.gemspec
Created May 16, 2017 17:50
A workaround for the gemname/version.rb practice that isn't broken exactly but I don't care for it.
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
# require "something/version" <- hells no
Gem::Specification.new do |spec|
spec.name = "something"
spec.version = "1.2.3" # <- makes sense here, right?
# ... gemspec guts ...
@samsm
samsm / dc_2017_biblio.md
Last active April 27, 2017 23:16 — forked from BaseCase/dc_2017_biblio.md
List of resources recommended or mentioned by the speakers at Deconstruct 2017

Deconstruct 2017 Bibliography

Here are all of the resources mentioned by Deconstruct 2017 speakers, along with who recommended what. Please post a comment if I missed something or have an error!

DC 2017 Speakers' Choice Gold Medalist

  • Seeing Like a State by James Scott

Books

  • Public Opinion by Walter Lippmann (Evan Czaplicki)
  • A Pattern Language by Christopher Alexander (Brian Marick)
  • Domain Driven Design by Eric Evans (Brian Marick)
class QuitWhenEmpty
def initialize(options=nil)
end
def call(worker, msg, queue)
yield
stop_disposible_worker_if_queue_empty!
end
private

Keybase proof

I hereby claim:

  • I am samsm on github.
  • I am samsm (https://keybase.io/samsm) on keybase.
  • I have a public key whose fingerprint is EDAC D3BE 23FB 52E2 B532 62FC 5C3A A2B3 0769 A1A3

To claim this, I am signing this object:

@samsm
samsm / cancan.rb
Created October 17, 2013 16:18
Hack for CanCan load_resource on Rails 4.
# Hack to get strong_parameters working
CanCan::ControllerResource
class CanCan::ControllerResource
protected
# Before using @params[whatever], CanCan will try calling whatever_params
def resource_params_by_namespaced_name
@controller.send(:"#{extract_key(namespaced_name)}_params")
rescue NoMethodError
@samsm
samsm / after.txt
Created April 29, 2013 15:16
Shorten stack trace file names
1) Error:
test_0001_should generate form with method=get(integration search_form_for):
ArgumentError: wrong number of arguments (1 for 2)
/spec/minitest_helper.rb:68:in `block (3 levels) in formed_class'
/gems/actionpack-3.2.9/lib/action_dispatch/routing/polymorphic_routes.rb:129:in `polymorphic_url'
/gems/actionpack-3.2.9/lib/action_dispatch/routing/polymorphic_routes.rb:135:in `polymorphic_path'
/gems/actionpack-3.2.9/lib/action_view/helpers/form_helper.rb:397:in `apply_form_for_options!'
/gems/actionpack-3.2.9/lib/action_view/helpers/form_helper.rb:370:in `form_for'
/lib/mundane-search/view_helpers.rb:10:in `search_form_for'
/spec/search_form_for_integration_spec.rb:15:in `block (2 levels) in <top (required)>'
@samsm
samsm / instance_eval_module_scope.rb
Last active December 14, 2015 20:19
Ruby instance_eval scope issue I don't understand.
module Bar
module Baz
end
def hi_there
puts 'Hi!!'
end
end
class Foo
@samsm
samsm / ability.rb
Created July 11, 2012 20:28
Authentication on middleware
class Ability
include CanCan::Ability
def initialize(user)
if user.admin?
can :manage, Resque
end
end
end