How to extract only needed attribute from an array

T

Thriving K.

I have an object called Tags with id and name ==> [ tag.id , tag.name]

| [1 , aaa] |

| [ 3 , bbb] |

| [ 5 , ccc] |

| [ 6 , dfd] |

| [ 7 , waf] |

| [ 11 , vee] |

There is an array with the list of tags , i just want to extract all
tag.id
as a new array, what is the easiest way to do
 
G

Gennady Bystritsky

PiAtLS0tLU9yaWdpbmFsIE1lc3NhZ2UtLS0tLQ0KPiBGcm9tOiBzdXBlcmRpc2Nvbm5lY3RAaG90
bWFpbC5jb20gW21haWx0bzpzdXBlcmRpc2Nvbm5lY3RAaG90bWFpbC5jb21dDQo+IFNlbnQ6IE1v
bmRheSwgQXVndXN0IDAzLCAyMDA5IDEwOjI3IFBNDQo+IFRvOiBydWJ5LXRhbGsgTUwNCj4gU3Vi
amVjdDogSG93IHRvIGV4dHJhY3Qgb25seSBuZWVkZWQgYXR0cmlidXRlIGZyb20gYW4gYXJyYXkN
Cj4gDQo+IA0KPiBJIGhhdmUgYW4gb2JqZWN0IGNhbGxlZCBUYWdzIHdpdGggaWQgYW5kIG5hbWUg
PT0+IFsgdGFnLmlkICwgdGFnLm5hbWVdDQo+IA0KPiB8IFsxICwgYWFhXSAgIHwNCj4gDQo+IHwg
WyAzICwgYmJiXSAgfA0KPiANCj4gfCBbIDUgLCBjY2NdICB8DQo+IA0KPiB8IFsgNiAsIGRmZF0g
IHwNCj4gDQo+IHwgWyA3ICwgd2FmXSAgfA0KPiANCj4gfCBbIDExICwgdmVlXSB8DQo+IA0KPiBU
aGVyZSBpcyBhbiBhcnJheSB3aXRoIHRoZSBsaXN0IG9mIHRhZ3MgLCBpIGp1c3Qgd2FudCB0byBl
eHRyYWN0IGFsbA0KPiB0YWcuaWQNCj4gYXMgYSBuZXcgYXJyYXksIHdoYXQgaXMgdGhlIGVhc2ll
c3Qgd2F5IHRvIGRvDQo+IC0tDQo+IFBvc3RlZCB2aWEgaHR0cDovL3d3dy5ydWJ5LWZvcnVtLmNv
bS8uDQoNCnRhZ3MubWFwIHsgfHR8IHQuaWQgfQ0KDQo=
 
J

Joel VanderWerf

Thriving said:
I have an object called Tags with id and name ==> [ tag.id , tag.name]

| [1 , aaa] |

| [ 3 , bbb] |

| [ 5 , ccc] |

| [ 6 , dfd] |

| [ 7 , waf] |

| [ 11 , vee] |

There is an array with the list of tags , i just want to extract all
tag.id
as a new array, what is the easiest way to do

Is this how your array looks?

a = [
[1 , :aaa],
[ 3 , :bbb],
[ 5 , :ccc],
[ 6 , :dfd],
[ 7 , :waf],
[ 11 , :vee]
]

p a.transpose[0] # ==> [1, 3, 5, 6, 7, 11]

Or:

p a.map {|b| b[0]}
 
R

Robert Klemme

2009/8/4 Thriving K. said:
I have an object called Tags with id and name =3D=3D> [ tag.id , tag.name= ]

| [1 , aaa] =A0 |

| [ 3 , bbb] =A0|

| [ 5 , ccc] =A0|

| [ 6 , dfd] =A0|

| [ 7 , waf] =A0|

| [ 11 , vee] |

There is an array with the list of tags , i just want to extract all
tag.id
as a new array, what is the easiest way to do

In any case you need method #map. In 1.9* you can even do use map(&:id):

irb(main):001:0> Tag =3D Struct.new :id, :name
=3D> Tag
irb(main):002:0> a =3D Array.new(5) {|i| Tag.new(i, "n"*i)}
=3D> [#<struct Tag id=3D0, name=3D"">, #<struct Tag id=3D1, name=3D"n">,
#<struct Tag id=3D2, name=3D"nn">, #<struct Tag id=3D3, name=3D"nnn">,
#<struct Tag id=3D4, name=3D"nnnn">]
irb(main):003:0> a.map(&:id)
=3D> [0, 1, 2, 3, 4]

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top