Skip to content

Instantly share code, notes, and snippets.

➜ ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
➜ ruby respond_to_missing.rb
Using #method works:
foo_bar
But #method_defined? doesn't:
method defined: false
And Module#instance_method raises a NameError:
respond_to_missing.rb:20:in `instance_method': undefined method `foo_bar' for class `Foo' (NameError)
from respond_to_missing.rb:20:in `<main>'
@myronmarston
myronmarston / output
Created July 28, 2012 22:16
Demonstration of changing the behavior of ruby's `rescue` so that it only handles shallow exceptions raised in directly called methods. Note that a bare `rescue` won't work with this, unfortunately. See https://twitter.com/garybernhardt/status/229323716
$ ruby --version
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.0]
$ ruby shallow_exceptions.rb
Exception handled: shallow exception
shallow_exceptions.rb:13:in `raise_shallow_exception': deep exception (StandardError)
from shallow_exceptions.rb:17:in `raise_deep_exception'
from shallow_exceptions.rb:27:in `try_handle_deep_exception'
from shallow_exceptions.rb:34:in `<main>'
# A sample Gemfile
source "https://rubygems.org"
gem "rspec", '2.9.0'
#!/bin/sh
set -e
TIMEFMT='%*E'
runtime=$(((bundle install --standalone && time rspec spec/acceptance/silo_spec.rb -e 'end-to-end') | tail -0) 2>&1)
ms=$(echo $runtime | ruby -e "puts Integer(Float(gets) * 1000)")
for for_i in [1, 2, 3]
end
puts "Variable from for: #{for_i.inspect}"
[1, 2, 3].each do |each_i|
end
puts "Variable from each: #{each_i.inspect}"
@myronmarston
myronmarston / triangle.rb
Created June 29, 2012 17:59 — forked from brenfrow/triangle.rb
triangle
TriangleError = Class.new(ArgumentError)
Triangle = Struct.new(:x, :y, :z) do
def initialize(*args)
super
validate
end
def type
TYPES.fetch(uniq_side_lengths)
@myronmarston
myronmarston / silo_client.rb
Created June 20, 2012 04:06
Example of allowing a faraday connection to be configured through middleware blocks.
module Silo
module Client
extend self
def configuration
@configuration ||= Configuration.new
end
def configure
require 'net/http'
response = Net::HTTP.start("httpstat.us") { |http| http.get("/204") }
puts "Body is: #{response.body.inspect}"
➜ rspec return_spec.rb --format doc
A
1
2 (FAILED - 1)
B
3
4
source :rubygems
gem "rspec", :git => "git://github.com/rspec/rspec.git"
gem "rspec-core", :git => "git://github.com/rspec/rspec-core.git"
gem "rspec-expectations", :git => "git://github.com/rspec/rspec-expectations.git"
gem "rspec-mocks", :git => "git://github.com/rspec/rspec-mocks.git"