Skip to content

Instantly share code, notes, and snippets.

@mislav
mislav / procs-vs-lambda.md
Last active March 26, 2021 18:34
Jim Weirich on the differences between procs and lambdas in Ruby

Jim Weirich:

This is how I explain it… Ruby has Procs and Lambdas. Procs are created with Proc.new { }, lambdas are created with lambda {} and ->() {}.

In Ruby 1.8, proc {} creates lambda, and Ruby 1.9 it creates procs (don't ask).

Lambdas use method semantics when handling parameters, procs use assignment semantics when handling parameters.

This means lambdas, like methods, will raise an ArgumentError when called with fewer arguments than they were defined with. Procs will simply assign nil to variables for arguments that were not passed in.

@soulcutter
soulcutter / capyconsole.rake
Created May 5, 2014 14:53
Capyconsole rake task
desc "Launch a Capybara session in a console"
task capyconsole: :environment do
require "capybara"
require "pry"
driver = case ENV.fetch('DRIVER', 'phantomjs')
when 'phantomjs'
require "capybara/poltergeist"
Capybara.register_driver :poltergeist_debug do |app|
@iamvery
iamvery / thoughts.md
Created May 8, 2014 02:04
Thoughts on contracts.ruby

I watching the demo/use cases video for contracts.ruby and recorded some thoughts.

Use case 1

The use case states that you can avoid bugs like needing string keys instead of symbol keys be enforcing a contract that requires the method input to be a string.

IMO you should instead create a flexible interface that will allow any "stringable" input to be given to the method. That is, use the implicit string conversion methods to_s to make a string of it for indexing into the hash.

Use case 2

@iamvery
iamvery / a-readme.md
Last active August 29, 2015 14:01
Conway's Game of Life, written in Ruby

Conway's Game of Life, written in Ruby

Run the game

ruby driver.rb

Run the specs

rspec game_of_life_spec.rb
@iamvery
iamvery / a-readme.md
Last active August 29, 2015 14:01
Conway's Game of Life, written in io

Conway's Game of Life, written in io

Run the game

io driver.io

Run the specs

io cell_spec.io
@iamvery
iamvery / conway.coffee
Created June 3, 2014 18:59
Game of Life WIP with @nybblr
# Live cells with < 2 living neighbors dies to underpop
# > 3 living neighbors dies to overpop
# Otherwise, lives!
#
# Dead cells with exactly 3 living neighbors is born
# . 0 .
# . 0 .
# . 0 .
#
@iamvery
iamvery / foo.rb
Last active August 29, 2015 14:06
require 'active_record'
class Foo < ActiveRecord::Base
establish_connection adapter: 'sqlite3', database: ':memory:'
connection.create_table :foos do |t|
t.string :name, null: false
end
#has_many :bars
params = { some: :thing }
oops = {}
def kwargs(some:)
puts some
end
def hash(args)
puts args.fetch(:some)
end
source 'https://rubygems.org'
gem 'activerecord'
gem 'sqlite3'
@iamvery
iamvery / Gemfile
Last active August 29, 2015 14:06
Scopes behavior in ActiveRecord 3
source 'https://rubygems.org'
gem 'activerecord', '~>3.0'
gem 'sqlite3'