0

just wondering if there're other alternatives to merging elements in an array from

[ ["time","Oct-1-2016"], ["message","test message"], ["host","localhost"] ]

to

["time=Oct-1-2016","message=test message","host=localhost"]

I've got it nailed down toarray.map {|k,v| "#{k}=#{v}"} and just wondering if there are any other ways of achieving the above without using the map function? Thanks yo!

8
  • 1
    Still using map you can do: arr.map {|s| s.join('=')} as well. Commented Sep 23, 2016 at 2:43
  • Yea, I definitely overcooked the k,v portion - thanks @sagarpandya82 Commented Sep 23, 2016 at 2:54
  • 1
    Of course: [ ["time","Oct-1-2016"], ["message","test message"], ["host","localhost"] ].each_with_object([]) { |a,b| b << a.join('=') }, but it is more cumbersome than simply map. Commented Sep 23, 2016 at 3:37
  • 2
    When you add a silly requirement like "without using the map function", you need to explain why, because the answer might be different. "I'm learning and we're not supposed to use it", "I am a Haskell survivor and those three letters standing side by side are aggravating me", "I've redefined map to always return false and I need help with gardenias holding my pacifier hostage" have wildly different responses. map is the best tool for the job; so without it you're working with a handicap. Commented Sep 23, 2016 at 3:58
  • 2
    If your endgame is to produce a query string, and since you've tagged this Rails, you might be interested in to_param which ActiveSupport mixes into Hash: array.to_h.to_param => "host=localhost&message=test+message&time=Oct-1-2016". Otherwise, it's completely unclear why you would impose a restriction like "without the use of map" on your question. Commented Sep 23, 2016 at 12:30

2 Answers 2

2

Let's say your array is a. Then try this solution:

[1] pry(main)> a = [ ["time","Oct-1-2016"], ["message","test message"], ["host","localhost"] ]
=> [["time", "Oct-1-2016"], ["message", "test message"], ["host", "localhost"]]

[2] pry(main)> a.map{|k, v| "#{k}=#{v}"}
=> ["time=Oct-1-2016", "message=test message", "host=localhost"]

Hm, I have no idea why map isn't works for you, but here is an example with inject:

[40] pry(main)> a.inject(Array.new){|acc, el| acc << el.join("="); acc}
=> ["time=Oct-1-2016", "message=test message", "host=localhost"]
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the attempt @retgoat, I'm looking for alternatives other than using the map function to achieve the result in [2].
@b1geyedeer I added one more example. Hope that will work for you.
Thanks, that's a good alternative, just for the record map works but as I've mentioned I was wondering if there are other ways to achieve the same output other than map. Sorry if I get everyone riled up.
No worries mate :) I'm glad that was able to help.
1

Here's one way using an infinite loop inside an Enumerator and by using the cycle method. Then use enum.take arr.size to get all your new elements in an array.

arr = [ ["time","Oct-1-2016"], ["message","test message"], ["host","localhost"] ]
ar  = arr.cycle

enum = Enumerator.new do |y|
  loop do
    y << ar.next.join('=')
  end
end

enum.take arr.size
#=> ["time=Oct-1-2016", "message=test message", "host=localhost"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.