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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-04-01 19:20
(Received via mailing list)
=> '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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-01 19:29
(Received via mailing list)
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
A4a4095ff08bd0fced3c3fddbeac743a?d=identicon&s=25 Cameron McBride (Guest)
on 2006-04-01 22:33
(Received via mailing list)
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
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2006-04-02 00:38
(Received via mailing list)
On 1-Apr-06, at 3:32 PM, Cameron McBride wrote:

>   %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.ca/~mike/

The "`Stok' disclaimers" apply.
430ea1cba106cc65b7687d66e9df4f06?d=identicon&s=25 David Vallner (Guest)
on 2006-04-02 02:29
(Received via mailing list)
DÅ?a 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
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-04-02 02:32
(Received via mailing list)
On 1 Apr 2006, at 18:27, James Edward Gray II wrote:

> => ["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
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-04-02 02:41
(Received via mailing list)
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
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-04-02 02:44
(Received via mailing list)
On 2 Apr 2006, at 01:26, David Vallner wrote:

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

Good timing - I'm munchy.
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2006-04-02 02:44
(Received via mailing list)
That's an interesting idea. Perhaps an options hash though in case
anyone thinks of alternatives:

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

T.
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-04-02 02:50
(Received via mailing list)
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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-02 03:00
(Received via mailing list)
Hi --

On Sun, 2 Apr 2006, Benjohn Barnes wrote:

>
> 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.

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 (dblack@wobblini.net)
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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-04-02 03:24
(Received via mailing list)
On Apr 1, 2006, at 7:32 PM, Benjohn Barnes wrote:

>>> the standard Enumerable?
> ! :) Thanks!
> irb(main):032:0> [a[0..-2].join(', '), a[-1]].join(' and ')
> 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
Ffcb418e17cac2873d611c2b8d8d891c?d=identicon&s=25 Benjohn Barnes (Guest)
on 2006-04-02 09:28
(Received via mailing list)
On 2 Apr 2006, at 01:58, dblack@wobblini.net wrote:

>
> 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
6dab365a82517fb694650a57ee88e4a4?d=identicon&s=25 joey__ (Guest)
on 2006-04-02 20:52
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
9a46755c817121b0571a563e5ab02090?d=identicon&s=25 Brian Buckley (brian)
on 2006-04-03 14:04
(Received via mailing list)
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
This topic is locked and can not be replied to.