Skip to content

Instantly share code, notes, and snippets.

@spencerroan
Created May 14, 2015 15:10
Show Gist options
  • Save spencerroan/8272ed27526cc05320c5 to your computer and use it in GitHub Desktop.
Save spencerroan/8272ed27526cc05320c5 to your computer and use it in GitHub Desktop.
confident ruby 3.2 examples
# tests for 3.2 of confident ruby
# inplicit vs explicit
#
require 'pry'
describe "section 3.2" do
context "string" do
class C
attr_accessor :name
def initialize(name)
@name = name
end
end
let(:object) { C.new("ruby") }
let(:io) { StringIO.new }
it "explicitly" do
io.write object
io.seek(0)
io.read.should eq "ruby"
end
it "implicitly" do
io.write object
io.seek(0)
io.read.should eq "ruby"
end
end
context "struct" do
require 'OStruct'
let(:struct) { OpenStruct.new(attributes) }
let(:attributes) {
{
name: "George Washington",
occupation: "President"
}
}
let(:more_attributes) {
{
birth_year: "1732",
birth_place: "westmoreland, va"
}
}
it "implicitly" do
result = more_attributes.merge(struct)
result.keys.size.should eq 4
end
it "explicitly" do
result = more_attributes.merge(struct)
result.keys.size.should eq 4
end
end
context "Regexp" do
let(:struct) { OpenStruct.new(name: "Miley Cyrus") }
let(:body) { "Disney characters such as Miley Cyrus." }
it do
body[struct].should eq "Miley Cyrus"
end
it do
body.gsub(struct, "Britney Spears").should eq "Disney characters such as Britney Spears."
end
it do
body.index(struct).should eq 26
end
it do
body.scan(struct).should eq ["Miley Cyrus"]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment