2

can I pass argument, to symbol passed to map method ?

Code:

[["a", "airway", "sarsaparilas"], ["a", "sarsaparilas", "airway"]].map!(&:join)

Results in:

["aairwaysarsaparilas", "asarsaparilasairway"]

Result I'm interested in:

["a airway sarsaparilas", "a sarsaparilas airway"]

I know how to use those methods separately, but this time I'm interested in symbol solution :)

  • See @UriAgassi's answer [here](http://stackoverflow.com/questions/23695653/can-you-supply-arguments-to-the-mapmethod-syntax-in-ruby/23711606#23711606). – Cary Swoveland Feb 12 '15 at 02:04

5 Answers5

3

You can define Array::to_proc to use an array instead a symbol:

class Array
  def to_proc
    ->(element) { element.send(*self) }
  end
end

And now you can use like this:

a=[["a", "airway", "sarsaparilas"], ["a", "sarsaparilas", "airway"]]
a.map &[:join] # => ["aairwaysarsaparilas", "asarsaparilasairway"] 
a.map &[:join, ' '] # => ["a airway sarsaparilas", "a sarsaparilas airway"]
Doguita
  • 15,403
  • 3
  • 27
  • 36
2

can I pass argument, to symbol

Someone created a gem for that:

require 'ampex'

arr = [["a", "airway", "sarsaparilas"], ["a", "sarsaparilas", "airway"]]

p arr.map!(&X.join(' '))

--output:--
["a airway sarsaparilas", "a sarsaparilas airway"]

You have to write &X. instead of :

7stud
  • 46,922
  • 14
  • 101
  • 127
1

The only way to do it is creating a method that does that.

def my_join(a)
  a.join(" ")
end

arr = [["a", "airway", "sarsaparilas"], ["a", "sarsaparilas", "airway"]]

arr.map &method(:my_join) # => ["a airway sarsaparilas", "a sarsaparilas airway"] 

Of course this does not seem much better then using a proc

my_join = proc {|a| a.join(" ") }

arr.map &my_join # => ["a airway sarsaparilas", "a sarsaparilas airway"] 
Ismael Abreu
  • 16,443
  • 6
  • 61
  • 75
1

You can simply pass a block in, instead of writing &:join:

a = [["a", "b", "c"], ["d", "e", "f"]]
a.map { |x| x.join(' ') }  # => ["a b c", "d e f"]

The &:join syntax is just a shortcut for making a block that calls .join on its argument, but that syntax is not appropriate in every situation.

David Grayson
  • 84,103
  • 24
  • 152
  • 189
1

I don't recommend doing it that way, but if you insist, this is a way to do it:

$, = " "
[["a", "airway", "sarsaparilas"], ["a", "sarsaparilas", "airway"]].map!(&:join)
#=> ["a airway sarsaparilas", "a sarsaparilas airway"]

If necessary, you can revert $, back to the default value after doing this.

$, = nil
sawa
  • 165,429
  • 45
  • 277
  • 381
  • Great answer, thanks, could you explain why you would not recommend using this approach ? – 098328f3e4f871e81bd35600fdcd56 Feb 11 '15 at 18:14
  • It is because using `Symbol#to_proc` with `&` is a shorthand for a simple method without arguments. Insisting to use it with arguments is not making it any shorter or more readable. For this particular case, you can do it as I wrote above, but I don't see that writing `$, = " "` is any simpler than writing the join block with the argument – sawa Feb 11 '15 at 18:16