Skip to content

Instantly share code, notes, and snippets.

@tribals
Last active April 13, 2016 17:23
Show Gist options
  • Save tribals/55c85c41d41caa266ab6a27aa5eb0421 to your computer and use it in GitHub Desktop.
Save tribals/55c85c41d41caa266ab6a27aa5eb0421 to your computer and use it in GitHub Desktop.
# assuming that `things` is an array of hashes which we want to process in some way
things = [
{ one: 42, two: 'spam' },
# ... and so on
{ one: 24, two: 'maps' }
]
things.map do |thing|
hsh = {}
hsh[:number] = thing[:one] * 2 + 3 # or whatewer
# more complex processing, so we cannot just return hash literal such as:
# { number: thing[:one], url: thing[:two] }
hsh[:url] = Nokogiri::HTML.fragment(thing[:two]).at('some_element')[:some_attr]
hsh
# that `hsh`, `hsh`, `hsh` really annoy me
end
@smathy
Copy link

smathy commented Apr 13, 2016

If your map is really as simple as you show, you can just use a literal hash:

things.map do |thing|
  {
    number: thing[:one] * 2 + 3,
    url: Nokogiri::HTML.fragment(thing[:two]).at('some_element')[:some_attr]
  }
end

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