[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

%w{tomato cheese ham pineapple}.join(', ', ' and '

benjohn

4/1/2006 5:20:00 PM

=> 'tomato, cheese, ham and pineapple'

Is something I'd like to be able to do, and I almost thought it's be
part of the standard. Is there already a standard way of getting that
(facets, perhaps?)? Any chance it'll make it in to the standard
Enumerable?

Cheers,
Benj




14 Answers

James Gray

4/1/2006 5:27:00 PM

0

On Apr 1, 2006, at 11:20 AM, Benjohn Barnes wrote:

> => 'tomato, cheese, ham and pineapple'
>
> Is something I'd like to be able to do, and I almost thought it's
> be part of the standard. Is there already a standard way of getting
> that (facets, perhaps?)? Any chance it'll make it in to the
> standard Enumerable?

>> list = %w{tomato cheese ham pineapple}
=> ["tomato", "cheese", "ham", "pineapple"]
>> [list[0..-2].join(", "), list[-1]].join(", and ")
=> "tomato, cheese, ham, and pineapple"

Hope that helps.

James Edward Gray II


Cameron McBride

4/1/2006 8:32:00 PM

0

On 4/1/06, Benjohn Barnes <benjohn@fysh.org> wrote:
> => 'tomato, cheese, ham and pineapple'
>
> Is something I'd like to be able to do, and I almost thought it's be
> part of the standard. Is there already a standard way of getting that
> (facets, perhaps?)? Any chance it'll make it in to the standard
> Enumerable?

another alternative:

%w{tomato cheese ham pineapple}.join(', ').sub(/, (\S+)$/,' and \1')

(I'm not Mr. Grammar, but I usually remove the last comma before the "and")

Cameron


Mike Stok

4/1/2006 10:37:00 PM

0


On 1-Apr-06, at 3:32 PM, Cameron McBride wrote:

> On 4/1/06, Benjohn Barnes <benjohn@fysh.org> wrote:
>> => 'tomato, cheese, ham and pineapple'
>>
>> Is something I'd like to be able to do, and I almost thought it's be
>> part of the standard. Is there already a standard way of getting that
>> (facets, perhaps?)? Any chance it'll make it in to the standard
>> Enumerable?
>
> another alternative:
>
> %w{tomato cheese ham pineapple}.join(', ').sub(/, (\S+)$/,' and \1')
>
> (I'm not Mr. Grammar, but I usually remove the last comma before
> the "and")

The Oxford comma is useful and acceptable. If there is any ambiguity
in the list then the comma can be most useful. To lift from http://
www.worldwidewords.org/qa/qa-oxf1.htm:

> The argument for using it is that it reduces the risk of ambiguity.
> For example, if you were to write “He studied Roman history,
> international politics and economics” it is not obvious whether
> international refers only to politics or also to economics. Putting
> the serial comma in removes that doubt. In practice, an alert
> writer will spot the potential problem and can often write around it.
>
> Perhaps the best argument for the serial comma is that apocryphal
> book dedication: “To my parents, Ayn Rand and God”.

Mike


--

Mike Stok <mike@stok.ca>
http://www.stok...

The "`Stok' disclaimers" apply.






David Vallner

4/2/2006 12:27:00 AM

0

Dna Sobota 01 Apríl 2006 19:20 Benjohn Barnes napísal:
> => 'tomato, cheese, ham and pineapple'
>
> Is something I'd like to be able to do, and I almost thought it's be
> part of the standard. Is there already a standard way of getting that
> (facets, perhaps?)? Any chance it'll make it in to the standard
> Enumerable?
>
> Cheers,
> Benj

A little late, but in spirit of when the thread started: I'd get the pizza
delivered ;)

David Vallner


benjohn

4/2/2006 12:32:00 AM

0


On 1 Apr 2006, at 18:27, James Edward Gray II wrote:

> On Apr 1, 2006, at 11:20 AM, Benjohn Barnes wrote:
>
>> => 'tomato, cheese, ham and pineapple'
>>
>> Is something I'd like to be able to do, and I almost thought it's
>> be part of the standard. Is there already a standard way of
>> getting that (facets, perhaps?)? Any chance it'll make it in to
>> the standard Enumerable?
>
> >> list = %w{tomato cheese ham pineapple}
> => ["tomato", "cheese", "ham", "pineapple"]
> >> [list[0..-2].join(", "), list[-1]].join(", and ")
> => "tomato, cheese, ham, and pineapple"
>
> Hope that helps.
>
> James Edward Gray II

! :) Thanks!

Argh, and it almost works in the edge cases, except for when there's
0 or 1 item in the list.

a = [1]
=> [1]
irb(main):030:0> [a[0..-2].join(', '), a[-1]].join(' and ')
=> " and 1"
irb(main):031:0> a = []
=> []
irb(main):032:0> [a[0..-2].join(', '), a[-1]].join(' and ')
=> " and "

When I was looking at this earlier, I found that..

def a_function(arg1, arg2 = arg1)
puts arg1, arg2
end

...worked as I'd expect, and was surprised that join didn't take an
optional second argument to be the separator between the last two items.

Cheers,
Benj



benjohn

4/2/2006 12:40:00 AM

0


On 1 Apr 2006, at 21:32, Cameron McBride wrote:

> .join(', ').sub(/, (\S+)$/,' and \1')

:) Groovy - and it works for the edge cases too...

irb(main):033:0> a=[1,2,3]
=> [1, 2, 3]
irb(main):034:0> a.join(', ').sub(/, (\S+)$/,' and \1')
=> "1, 2 and 3"
irb(main):035:0> a=[1,2]
=> [1, 2]
irb(main):036:0> a.join(', ').sub(/, (\S+)$/,' and \1')
=> "1 and 2"
irb(main):037:0> a=[1]
=> [1]
irb(main):038:0> a.join(', ').sub(/, (\S+)$/,' and \1')
=> "1"
irb(main):039:0> a=[]
=> []
irb(main):040:0> a.join(', ').sub(/, (\S+)$/,' and \1')
=> ""

Thanks.

It still seems like a nice one to have in the standard, to me.

Cheers,
B

Trans

4/2/2006 12:41:00 AM

0

That's an interesting idea. Perhaps an options hash though in case
anyone thinks of alternatives:

join( ', ', :last => ' and ')

T.

benjohn

4/2/2006 12:43:00 AM

0


On 2 Apr 2006, at 01:26, David Vallner wrote:

> Dna Sobota 01 Apríl 2006 19:20 Benjohn Barnes napísal:
>> => 'tomato, cheese, ham and pineapple'
>>
>> Is something I'd like to be able to do, and I almost thought it's be
>> part of the standard. Is there already a standard way of getting that
>> (facets, perhaps?)? Any chance it'll make it in to the standard
>> Enumerable?
>>
>> Cheers,
>> Benj
>
> A little late, but in spirit of when the thread started: I'd get
> the pizza
> delivered ;)

Good timing - I'm munchy.



benjohn

4/2/2006 12:51:00 AM

0


On 2 Apr 2006, at 01:40, Benjohn Barnes wrote:
> It still seems like a nice one to have in the standard, to me.

Is there somewhere for suggestion new features? I'm sure I remember
seeing that, but I'm having trouble tracking it down.

Cheers,
Ben


dblack

4/2/2006 12:58:00 AM

0