Pairing Data 2-D array

16 views
Skip to first unread message

Jan Yo

unread,
Apr 14, 2014, 5:12:29 PM4/14/14
to rubyonra...@googlegroups.com
I have class A.
I have a class B with attributes c and d.
A has many Bs.

I get an instance of A using
a = find_by....

I can get a listing (enumeration) of values for attribute b using map
function.

a.b.map{ |x| x.c}

c1
c2
c3

However, I want c and d returned and paired together in an array like a
2-d array.

c1 d1
c2 d2
c3 c3

What's the best way to do this? Map won't work, correct?

--
Posted via http://www.ruby-forum.com/.

Antônio Augusto Sousa Britto

unread,
Apr 14, 2014, 9:13:04 PM4/14/14
to rubyonra...@googlegroups.com
You can do an array of arrays:

a.b.map {|x| [x.c, x.d]}


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d523e7b0f6a6396078ba06c074e8e9c7%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.



--
Att, Antônio Augusto de Sousa Britto - 43ª turma de Ciência da Computação - UFU
Reply all
Reply to author
Forward
0 new messages