# Outputting a string from an array of objects

**URL:** <https://www.ruby-forum.com/t/outputting-a-string-from-an-array-of-objects/203670>\
**Category:** Rails\
**Created:** [February 24, 2011, 6:59pm UTC](https://www.ruby-forum.com/t/outputting-a-string-from-an-array-of-objects/203670 "2011-02-24T18:59:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 24, 2011, 6:59pm UTC](https://www.ruby-forum.com/t/outputting-a-string-from-an-array-of-objects/203670/1 "2011-02-24T18:59:49Z")

</div>

Hi All,

Newbie here working with an existing Rails project. I’m trying to create  
a method that will return a simple string of object values.

I have an app that has companies that each are assigned to one or more  
branches. Then I have a contact who is assigned to a company. I’ve  
created an association with belongs\_to for both the company and branch,  
and the branch and company has their own class in AR.

So

def get\_company\_branches

my\_branches = self.company.branches  
my\_list = my\_branches.each { |x| puts x.branch\_name }  
return my\_list  
end

For some reason this returns an object id (the raw ruby object id I  
think) not the string i am looking for. Any suggestions?

Best,

Peter

---

<div class="post-metadata">

**Author:** ![Peter\_D\_Bethke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e495f1/32.png) [@Peter\_D\_Bethke](https://www.ruby-forum.com/u/Peter_D_Bethke)\
**Post date:** [February 24, 2011, 7:04pm UTC](https://www.ruby-forum.com/t/outputting-a-string-from-an-array-of-objects/203670/2 "2011-02-24T19:04:12Z")

</div>

On Feb 24, 5:07pm, Peter D Bethke [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> my\_branches = self.company.branches  
> my\_list = my\_branches.each { |x| puts x.branch\_name }  
> return my\_list  
> end
> 
> For some reason this returns an object id (the raw ruby object id I think) not  
> the string i am looking for. Any suggestions?

puts just dumps things to stdout, so you definitely don’t want to be  
using that here.  
If you want to create an array based on the value of each object in a  
collection then you should use collect (also available as map). It  
creates a new array containing the result of evaluating the block for  
each object of the original collection.

Fred

---

<div class="post-metadata">

**Author:** ![Peter\_D\_Bethke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e495f1/32.png) [@Peter\_D\_Bethke](https://www.ruby-forum.com/u/Peter_D_Bethke)\
**Post date:** [February 24, 2011, 7:17pm UTC](https://www.ruby-forum.com/t/outputting-a-string-from-an-array-of-objects/203670/3 "2011-02-24T19:17:42Z")

</div>

On 24 February 2011 17:07, Peter D Bethke [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def get\_company\_branches  
> my\_branches = self.company.branches  
> my\_list = my\_branches.each { |x| puts x.branch\_name }  
> return my\_list  
> end

You might find that something like this is what you’re after:

def get\_company\_branches  
self.company.branches.collect(&:branch\_name).join(", ") if  
self.company && self.company.branches  
end

Have a look at the methods available to Array and Enumerable in both  
the Rails and Ruby apis.

---

<div class="post-metadata">

**Author:** ![Peter\_D\_Bethke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e495f1/32.png) [@Peter\_D\_Bethke](https://www.ruby-forum.com/u/Peter_D_Bethke)\
**Post date:** [February 24, 2011, 7:25pm UTC](https://www.ruby-forum.com/t/outputting-a-string-from-an-array-of-objects/203670/4 "2011-02-24T19:25:55Z")

</div>

On 24 February 2011 18:12, Peter D Bethke [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Fred!
> 
> Does this look right? I’m still getting an error for some reason:

Always post the error… just saying “I’ve got one” doesn’t help  
people point out where you might be having problems (although in this  
case, it’s ruby syntax… check the API for the “.collect” method 🙂

def get\_company\_branches  
my\_branches = self.company.branches # you don’t really need these  
intermediate variables… just use self.branches.collect…  
my\_list = my\_branches.collect { |x| x.branch\_name }  
return my\_list.join(", ") # “collect” returns an array, so we can  
join the array into a string, or maybe use the “to\_sentence” extension  
to prettify it  
end

…my previous method just does the same, a little more compact.

---

<div class="post-metadata">

**Author:** ![Peter\_D\_Bethke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e495f1/32.png) [@Peter\_D\_Bethke](https://www.ruby-forum.com/u/Peter_D_Bethke)\
**Post date:** [February 24, 2011, 7:20pm UTC](https://www.ruby-forum.com/t/outputting-a-string-from-an-array-of-objects/203670/5 "2011-02-24T19:20:52Z")

</div>

Thanks Fred!

Does this look right? I’m still getting an error for some reason:

def get\_company\_branches

my\_branches = self.company.branches  
my\_list = my\_branches.branch\_name.collect! { |x| x + “,” }  
return my\_list  
end

looking for it to output a concatenated string - still erroring out tho.  
☹

Peter

---

<div class="post-metadata">

**Author:** ![Peter\_D\_Bethke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e495f1/32.png) [@Peter\_D\_Bethke](https://www.ruby-forum.com/u/Peter_D_Bethke)\
**Post date:** [February 24, 2011, 11:21pm UTC](https://www.ruby-forum.com/t/outputting-a-string-from-an-array-of-objects/203670/6 "2011-02-24T23:21:45Z")

</div>

On Feb 24, 6:12pm, Peter D Bethke [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Fred!
> 
> Does this look right? I’m still getting an error for some reason:
> 
> def get\_company\_branches
> 
> my\_branches = self.company.branches  
> my\_list = my\_branches.branch\_name.collect! { |x| x + “,” }  
> return my\_list  
> end

You need to call collect (you don’t want collect!) on the array, not  
on the branch\_name for an individual branch

Fred
