Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

implode function?

11 views
Skip to first unread message

Julian Leviston

unread,
Sep 8, 2005, 11:18:04 PM9/8/05
to
Is there are similar thing to the PHP version of implode in ruby?

I want this to happen...

z = %w[yeah, cool, mad, awesome, funky]

x = z.implode

x

'yeahcoolmadeawesomefunky'

x = z.implode(', ')

x

'yeah, cool, mad, awesome, funky'

I guess I'll just go write one.

Julian.


manveru

unread,
Sep 8, 2005, 11:20:55 PM9/8/05
to
Julian Leviston wrote:

z = %w[yeah cool mad awesome funky]
=> ["yeah", "cool", "mad", "awesome", "funky"]
x = z.join(', ')
=> "yeah, cool, mad, awesome, funky"

Gennady Bystritsky

unread,
Sep 8, 2005, 11:50:35 PM9/8/05
to

On Sep 8, 2005, at 20:18, Julian Leviston wrote:

> Is there are similar thing to the PHP version of implode in ruby?
>
> I want this to happen...
>
> z = %w[yeah, cool, mad, awesome, funky]
>
> x = z.implode

x = z.join
or simply
x = z.to_s

>
> x
>
> 'yeahcoolmadeawesomefunky'
>
> x = z.implode(', ')

x = z.join(', ')

Lyndon Samson

unread,
Sep 9, 2005, 12:40:29 AM9/9/05
to
Allways assume ruby has nicer method/function names than PHP :-)

Florian Frank

unread,
Sep 12, 2005, 3:20:17 PM9/12/05
to
Julian Leviston wrote:

> Is there are similar thing to the PHP version of implode in ruby?
>
> I want this to happen...
>
> z = %w[yeah, cool, mad, awesome, funky]

z = %w[yeah cool mad awesome funky]

Additionally to what the others have already said, you can use the
*-operator here:

x = z * ''

x = z * ', '

--
Florian Frank

Martin DeMello

unread,
Sep 12, 2005, 4:09:24 PM9/12/05
to
Florian Frank <fl...@nixe.ping.de> wrote:
>
> z = %w[yeah cool mad awesome funky]
>
> Additionally to what the others have already said, you can use the
> *-operator here:
>
> x = z * ''
>
> x = z * ', '

Shame there's no corresponding String#/

martin

Florian Frank

unread,
Sep 12, 2005, 4:29:02 PM9/12/05
to
Martin DeMello wrote:

>Shame there's no corresponding String#/
>
>

Ha, now there is:

class String; alias / split; end


'a,b,c' / ',' # => ['a', 'b', 'c']

This looks strange, though:

'a,b,c' / /,/ # => ['a', 'b', 'c']

--
Florian Frank

Austin Ziegler

unread,
Sep 12, 2005, 4:31:41 PM9/12/05
to

class String
def /(val)
self.split(val)
end
end

-austin
--
Austin Ziegler * halos...@gmail.com
* Alternate: aus...@halostatue.ca


David A. Black

unread,
Sep 12, 2005, 5:48:18 PM9/12/05
to
Hi --

On Fri, 9 Sep 2005, Julian Leviston wrote:

> Is there are similar thing to the PHP version of implode in ruby?
>
> I want this to happen...
>
> z = %w[yeah, cool, mad, awesome, funky]

See the other answers for the answer(s), but also, note that the
elements in that array are:

"yeah," "cool," "mad," "awesome," "funky"

i.e., the commas are part of the words (which I think isn't what you
wanted :-)


David

--
David A. Black
dbl...@wobblini.net


0 new messages