Skip to content

Instantly share code, notes, and snippets.

@maetl
Created March 30, 2015 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maetl/df441f02370ec3100423 to your computer and use it in GitHub Desktop.
Save maetl/df441f02370ec3100423 to your computer and use it in GitHub Desktop.
Interesting quirk with Structs in Transproc
# spec/integration/hash_spec.rb
describe 'combining transformations' do
it 'applies functions to a struct' do
symbolize_keys = Transproc(:symbolize_keys)
map = Transproc(:map_hash, user_name: :name, user_email: :email)
transformation = symbolize_keys + map
InputUser = Struct.new(:user_name, :user_email)
input = InputUser.new('Jade', 'jade@doe.org')
output = { name: 'Jade', email: 'jade@doe.org' }
result = transformation[input]
expect(result).to eql(output)
end
end
Failures:
1) Hash mapping with Transproc combining transformations applies functions to a struct
Failure/Error: result = transformation[input]
ArgumentError:
odd number of arguments for Hash
# ./lib/transproc/hash.rb:3:in `[]'
# ./lib/transproc/hash.rb:3:in `block in <module:Transproc>'
# ./lib/transproc/function.rb:16:in `[]'
# ./lib/transproc/function.rb:16:in `block in compose'
# ./lib/transproc/function.rb:11:in `[]'
# ./lib/transproc/function.rb:11:in `call'
# ./spec/integration/hash_spec.rb:157:in `block (3 levels) in <top (required)>'
Finished in 0.06424 seconds (files took 1.66 seconds to load)
52 examples, 1 failure
@maetl
Copy link
Author

maetl commented Mar 30, 2015

This is expected behaviour. Only hashes are fully supported. Structs just happen to work in some cases because they declare the [] method as an alternative way of accessing attributes.

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