Skip to content

Instantly share code, notes, and snippets.

class Sentence
def test(sen)
first = sen[0]
last = sen.split('').last
rest = sen[1..-1]
word_arr = sen.split
word_arr_len = word_arr.length
unless first == first.upcase then
puts "the first letter is not in uppercase"
@myronmarston
myronmarston / stack_overflow_debugger.rb
Created June 1, 2015 22:51
Stack overflow debugger (since Ruby doesn't provide the whole stack in this case)
max_stack_frames = 500
TooManyStackFrames = Class.new(StandardError)
TracePoint.new(:call) do |tp|
if caller.size >= max_stack_frames
raise TooManyStackFrames, "Stack has exceeded #{max_stack_frames} frames"
end
end.enable
@myronmarston
myronmarston / raise_error_spec.rb
Created April 10, 2015 16:58
Demonstration of using matcher for the first argument to `raise_error`
RSpec::Matchers.define :an_exception_caused_by do |cause|
match do |exception|
cause === exception.cause
end
end
expect {
begin
"foo".gsub # requires 2 args
rescue ArgumentError
@myronmarston
myronmarston / a_string_including.rb
Created April 9, 2015 17:35
Example of using `a_string_including`
expect {
expect(the_dbl).to have_received(:expected_method).with(:one).once
}.to raise_error(Expectations::ExpectationNotMetError,
a_string_including("expected: 1 time",
"received: 2 times"))
# A sample Gemfile
source "https://rubygems.org"
gem 'rspec'
coverage
require_relative '../order.rb'
describe Order do
def new_order(input)
order = Order.new
allow(order).to receive(:input).and_return(input)
order
end
it 'gets dish number as input' do
class SomeEntity
def run
transaction do
mark_non_eligible
make_invoice_batch
send_batch_email
end
end
def transaction

Haven't dived into RSpec but is it not possible for expect to accept either args or blocks, and process them internally?

First off, expect does accept either an arg or a block already. But if it's a block (or a proc/lambda arg), expect does not call the block automatically -- it just passes the block to the matcher and allows the matcher to call it if it wants. This is necessary because some matchers (the block matchers like raise_error, change, etc) must wrap the block in some extra logic to work properly because they deal in side effects, not expression return values.

@myronmarston
myronmarston / Gemfile-3-1
Last active August 29, 2015 14:14
Demonstration of object allocation improvements in RSpec 3.2
source "https://rubygems.org"
gem 'rspec-core', "~> 3.1.7"
gem 'allocation_stats'