implode function?

  • Thread starter Julian Leviston
  • Start date
J

Julian Leviston

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

manveru

Julian said:
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.
z = %w[yeah cool mad awesome funky]
=> ["yeah", "cool", "mad", "awesome", "funky"]
x = z.join(', ')
=> "yeah, cool, mad, awesome, funky"
 
G

Gennady Bystritsky

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(', ')
 
L

Lyndon Samson

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

Allways assume ruby has nicer method/function names than PHP :)

------=_Part_494_7222613.1126240827378--
 
F

Florian Frank

Julian said:
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 * ', '
 
M

Martin DeMello

Florian Frank said:
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
 
F

Florian Frank

Martin said:
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']
 
A

Austin Ziegler

Florian Frank said:
z =3D %w[yeah cool mad awesome funky]
Additionally to what the others have already said, you can use the
*-operator here:
x =3D z * ''
x =3D z * ', '
Shame there's no corresponding String#/

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

-austin
--=20
Austin Ziegler * (e-mail address removed)
* Alternate: (e-mail address removed)
 
D

David A. Black

Hi --

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
 

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,183
Messages
2,570,966
Members
47,513
Latest member
JeremyLabo

Latest Threads

Top