# Format of a string, array or hash

**URL:** <https://www.ruby-forum.com/t/format-of-a-string-array-or-hash/175048>\
**Category:** Rails\
**Created:** [September 7, 2009, 12:33am UTC](https://www.ruby-forum.com/t/format-of-a-string-array-or-hash/175048 "2009-09-07T00:33:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [September 7, 2009, 12:33am UTC](https://www.ruby-forum.com/t/format-of-a-string-array-or-hash/175048/1 "2009-09-07T00:33:18Z")

</div>

Hello there,  
I do not know if my topic is right so I’ll explain the problem :

I have a Project model :

class Project \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :services  
end

In my views. I would like to list the services in the title of the page.

I did :  
\<% for service in @project.services %-\>\<%= service.name\_fr.downcase -%\>,  
\<% end -%\>.

The problem is with the listing itself, it puts this :  
{Project.name} required {service.name1}, {service.name2},  
{service.name3},.

There is a problem with the last comma. It looks awful from a web  
developer view.

I would like :  
{Project.name} required {service.name1}, {service.name2} and  
{service.name3}.

Notice the attribute Â«andÂ» and Â«dotÂ» at the end

How do you call that thing I am looking for?  
Is there a way to do it like the second exemple?

Thx a lot for your time and help.

Hugues

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [September 7, 2009, 1:19am UTC](https://www.ruby-forum.com/t/format-of-a-string-array-or-hash/175048/2 "2009-09-07T01:19:07Z")

</div>

On Sun, Sep 6, 2009 at 3:33 PM, Hugues  
Brunelle[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would like :  
> {Project.name} required {service.name1}, {service.name2} and  
> {service.name3}.

Something like this should work:

@project.services.collect {|service| service.name}.join(", ").gsub(/,  
([A-Za-z]+)$/,’ and \1.')

:: though you might need to tweak that regex, depending on what’s  
in a service name…

## HTH,

Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [September 7, 2009, 3:00am UTC](https://www.ruby-forum.com/t/format-of-a-string-array-or-hash/175048/3 "2009-09-07T03:00:20Z")

</div>

@project.services.collect{|s| s.name}.to\_sentence

On Sep 6, 3:33 pm, Hugues B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [September 7, 2009, 3:37am UTC](https://www.ruby-forum.com/t/format-of-a-string-array-or-hash/175048/4 "2009-09-07T03:37:46Z")

</div>

On Sun, Sep 6, 2009 at 5:47 PM, Eric[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @project.services.collect{|s| s.name}.to\_sentence

Sweet, didn’t know that one. Let’s add the period, though:

@project.services.collect{|s| s.name}.to\_sentence.concat(“.”)

Et voila, another good day’s work 🙂

–  
Hassan S. ------------------------ removed\_email\_address@domain.invalid  
twitter: @hassan

---

<div class="post-metadata">

**Author:** ![hugues](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f04885/32.png) [@hugues](https://www.ruby-forum.com/u/hugues)\
**Post date:** [September 7, 2009, 8:48pm UTC](https://www.ruby-forum.com/t/format-of-a-string-array-or-hash/175048/5 "2009-09-07T20:48:27Z")

</div>

Hello Hassan and Eric,  
Outstanding! Thanks a lot, It works perfectly.

Hugues
