drlelon
November 20, 2006, 4:33pm
1
Hi,
I have a hash that contains the following keys/values:
[#<Contact_List:0x2607150 @attributes={“contact_id”=>“4”}>,
#<Contact_List:0x2607114 @attributes={“contact_id”=>“8”}>]
I want to create an array containing the contact_id values. What’s an
easy way to do this?
Thanks,
David
drlelon
November 20, 2006, 4:40pm
2
David L. wrote:
Hi,
I have a hash that contains the following keys/values:
[#<Contact_List:0x2607150 @attributes={“contact_id”=>“4”}>,
#<Contact_List:0x2607114 @attributes={“contact_id”=>“8”}>]
I want to create an array containing the contact_id values. What’s an
easy way to do this?
Try this:
result = x.inject([]) {|a,h| a << h.values[0]; a}
(x is the array of hashes)
Cheers,
Peter
__
http://www.rubyrailways.com
drlelon
November 20, 2006, 4:40pm
3
On Tue, 21 Nov 2006 00:33:33 +0900, David L. wrote:
Thanks,
David
irb(main):001:0> {1=>2,3=>4}.values
=> [2, 4]
irb(main):002:0> {1=>2,3=>4}.keys
=> [1, 3]
drlelon
November 20, 2006, 4:44pm
4
Peter S. wrote:
Try this:
result = x.inject([]) {|a,h| a << h.values[0]; a}
Sorry I have overlooked a little detail… My solution would work if the
original array would contain
[{“contact_id”=>“4”}, {“contact_id”=>“8”}]
For the original question, the answer should be probably
result = x.inject([]) {|a,h| a << h.attributes.values[0]; a}
HTH,
Peter
__
http://www.rubyrailways.com
drlelon
November 20, 2006, 4:44pm
5
On Nov 20, 2006, at 9:39 AM, Peter S. wrote:
result = x.inject([]) {|a,h| a << h.values[0]; a}
Any time you see inject() used as it is above, the intention was
really map():
result = x.map { |h| h.values[0] }
Same thing.
James Edward G. II
drlelon
November 20, 2006, 4:48pm
6
On Tue, 21 Nov 2006 00:33:33 +0900, David L. wrote:
Thanks,
David
Sorry. I should look at the code you have before I respond.
You have an array full of objects. I’m not sure how to get a contact_id
out of a Contact_List, but let’s assume you’ve defined a method that
gets
you the contact id, called Contact_List#id
Then, you would get the contact id by
[#<Contact_List:0x2607150 @attributes={“contact_id”=>“4”}>,
#<Contact_List:0x2607114 @attributes={“contact_id”=>“8”}>].collect do
|x|
x.id
end
drlelon
November 20, 2006, 4:52pm
7
On Mon, 20 Nov 2006 15:33:33 -0000, David L. [email protected]
wrote:
Thanks,
David
Assuming Contact_List (aside: why an underscored class name?) has an
attr_accessor for ‘attributes’, you might try:
a.map { |clist| clist.attributes['contact_id'] }
(where ‘a’ is the array you show above).
drlelon
November 20, 2006, 4:46pm
8
On 20.11.2006 16:33, David L. wrote:
Hi,
I have a hash that contains the following keys/values:
[#<Contact_List:0x2607150 @attributes={“contact_id”=>“4”}>,
#<Contact_List:0x2607114 @attributes={“contact_id”=>“8”}>]
Actually it seems you rather have an Array of Contatc_List instances.
I want to create an array containing the contact_id values. What’s an
easy way to do this?
your_array.map {|cl| cl.attributes[“contact_id”]}
Regards
robert
drlelon
November 20, 2006, 5:11pm
9
On Nov 20, 2006, at 10:50 AM, Ross B. wrote:
I want to create an array containing the contact_id values.
a.map { |clist| clist.attributes[‘contact_id’] }
(where ‘a’ is the array you show above).
–
Ross B. - [email protected]
Well, the OP asked for easy and what’s easier than Symbol#to_proc
(or am I reading Rails and ActiveRecord into this question where it’s
missing?)
a = [#<Contact_List:0x2607150 @attributes={“contact_id”=>“4”}>,
#<Contact_List:0x2607114 @attributes={“contact_id”=>“8”}>]
a.map(&:contact_id)
=> [4, 8]
-Rob
Rob B. http://agileconsultingllc.com
[email protected]
drlelon
November 21, 2006, 7:11am
10
David L. wrote:
Hi,
I have a hash that contains the following keys/values:
[#<Contact_List:0x2607150 @attributes={“contact_id”=>“4”}>,
#<Contact_List:0x2607114 @attributes={“contact_id”=>“8”}>]
I want to create an array containing the contact_id values. What’s an
easy way to do this?
contact_lists.map { |each| each.attributes[“contact_id”] }
Is each of those objects really a ContactList if each only has a single
contact ID?
Take care.
drlelon
November 22, 2006, 6:53pm
11
Personally I’d do:
c_ids=[]
for l in @attributes.values
c_ids << l
end
drlelon
November 23, 2006, 4:37pm
12
c_ids = my_hash.values.collect{|value| value.attributes[‘contact_id’]}
2006/11/23, [email protected] [email protected] :