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

B

Benjohn Barnes

=> '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
 
J

James Edward Gray II

=> '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
 
C

Cameron McBride

=3D> '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
 
M

Mike Stok

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 =20
the "and")

The Oxford comma is useful and acceptable. If there is any ambiguity =20=

in the list then the comma can be most useful. To lift from http://=20
www.worldwidewords.org/qa/qa-oxf1.htm:
The argument for using it is that it reduces the risk of ambiguity. =20=
For example, if you were to write =93He studied Roman history, =20
international politics and economics=94 it is not obvious whether =20
international refers only to politics or also to economics. Putting =20=
the serial comma in removes that doubt. In practice, an alert =20
writer will spot the potential problem and can often write around it.

Perhaps the best argument for the serial comma is that apocryphal =20
book dedication: =93To my parents, Ayn Rand and God=94.

Mike


--=20

Mike Stok <[email protected]>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.
 
D

David Vallner

D=C5=88a Sobota 01 Apr=C3=ADl 2006 19:20 Benjohn Barnes nap=C3=ADsal:
=3D> '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=
=20
delivered ;)

David Vallner
 
B

Benjohn Barnes

=> '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
 
B

Benjohn Barnes

--Apple-Mail-15-274981736
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed


.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
--Apple-Mail-15-274981736--
 
T

Trans

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

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

T.
 
B

Benjohn Barnes

D=C5=88a Sobota 01 Apr=C3=ADl 2006 19:20 Benjohn Barnes nap=C3=ADsal:

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

Good timing - I'm munchy.
 
B

Benjohn Barnes

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
 
D

dblack

Hi --

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

Once you've really determined that you can make a case for it, you can
prepare an RCR (Ruby Change Request) and post it to
http://www.rcrchive.net (now running on my new server; if the name
resolves to the old one, wait a day or two).


David

--
David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
 
L

Logan Capaldo

=> '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

This works in all the edge cases also, but its a bit verbose:
class Array
def multi_join(*args)
if args.empty?
join
elsif args.length == 1
join(args[0])
else
accum = ""
normal = args[0]
last = args[1]
index_of_second_to_last_item = length - 2
index_of_last_item = length - 1
each_with_index do |item, count|
accum << item.to_s
if count == index_of_last_item
break
elsif count == index_of_second_to_last_item
accum << last
else
accum << normal
end
end
accum
end
end
end
 
B

Benjohn Barnes

Hi --



Once you've really determined that you can make a case for it, you can
prepare an RCR (Ruby Change Request) and post it to
http://www.rcrchive.net (now running on my new server; if the name
resolves to the old one, wait a day or two).

Thank you, I'll do that. I was wracking my brains trying to remember
that last night, and all I could come up with to Google was feature
request.

Cheers,
Benjohn
 
J

joey__

class Array
def join_sentence
case length
when 1
self[0]
when 2
"#{self[0]} and #{self[1]}"
else
"#{self[0...-1].join(', ')} and #{self[-1]}"
end
end
end
 
B

Brian Buckley

------=_Part_27015_12388787.1144065718388
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

One more tweak for the empty array if "" makes sense.

class Array
def join_sentence
case length
when 0: ""
when 1: self[0]
else [self[0..-2].join(", "), self[-1]].join(" and ")
end
end
end

class Array
def join_sentence
case length
when 1
self[0]
when 2
"#{self[0]} and #{self[1]}"
else
"#{self[0...-1].join(', ')} and #{self[-1]}"
end
end
end

------=_Part_27015_12388787.1144065718388--
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top