Skip to content

Instantly share code, notes, and snippets.

@alexch
Forked from anonymous/aliases.rb
Created August 4, 2013 19:07
Show Gist options
  • Save alexch/6151497 to your computer and use it in GitHub Desktop.
Save alexch/6151497 to your computer and use it in GitHub Desktop.
# encoding: utf-8
module Kernel
alias :λ :lambda
end
class Hash
alias :+ :merge
alias :<< :merge!
end
# encoding: utf-8
require './aliases.rb'
describe 'cool aliases' do
describe 'lambda' do
it 'works with a regular call' do
p = lambda do
true
end
p.call.should == true
end
it 'works with an aliased call' do
p = λ do # unicode 03BB
true
end
p.call.should == true
end
end
describe 'hash methods' do
describe '+' do
it 'concatenates two arrays' do
([1] + [2]).should == [1,2]
end
it 'concatenates two hashes, aliasing merge' do
({a: 1} + {b: 2}).should == {a: 1, b: 2}
end
it 'makes a new object' do
a = {a: 1}
b = {b: 2}
(a + b).should == {a: 1, b: 2}
(a + b).object_id.should_not == a.object_id
end
end
describe '<<' do
it 'smooshes an item into an array' do
([1] << 2).should == [1,2]
end
it 'smooshes one hash into another, aliasing merge!' do
({a: 1} << {b: 2}).should == {a: 1, b: 2}
end
it 'mutates the original left-side object' do
a = {a: 1}
b = {b: 2}
(a << b).should == {a: 1, b: 2}
(a << b).object_id.should == a.object_id
end
it 'overwrites the left side with the right side' do
({a: 1} << {a: 2}).should == {a: 2}
end
end
end
end
@alexch
Copy link
Author

alexch commented Oct 22, 2014

todo: π = Math::PI

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment