Skip to content

Instantly share code, notes, and snippets.

@ipoval
Last active August 29, 2015 14:16
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 ipoval/bf95dedd5a2a6046ada7 to your computer and use it in GitHub Desktop.
Save ipoval/bf95dedd5a2a6046ada7 to your computer and use it in GitHub Desktop.
Array#to_proc # [ { name: "A" }, { name: "B" } ].map(&[:name]) # => [ "A", "B" ]

INSTALLATION

echo "gem 'array_to_proc', git: 'git://gist.github.com/bf95dedd5a2a6046ada7.git'" >> Gemfile

or

git clone git@gist.github.com:/bf95dedd5a2a6046ada7.git
gem build array_to_proc.gemspec
gem install --local array_to_proc-0.0.1.gem

EXAMPLE OF USAGE

irb -r array_to_proc
[ { name: "A" }, { name: "B" } ].map(&[:name]) # => [ "A", "B" ]
Gem::Specification.new do |s|
s.name = 'array_to_proc'
s.version = '0.0.1'
s.platform = Gem::Platform::RUBY
s.author = '@ipoval'
s.email = 'ipoval@ya.ru'
s.summary = '[ { name: "A" }, { name: "B" } ].map(&[:name]) # => [ "A", "B" ]'
s.description = '[ { name: "A" }, { name: "B" } ].map(&[:name]) # => [ "A", "B" ]'
s.files = ['array_to_proc.rb']
s.require_path = '.'
end
# [ { name: "A" }, { name: "B" } ].map(&[:name]) # => [ "A", "B" ]
class Array
def to_proc
->(h) { length == 1 ? h[first] : h.values_at(*self) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment