[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

String concatenation in Ruby

Jagadeesh

5/25/2009 5:54:00 AM

Hi,
I am looking for something similar to join in perl. I am doing
[sample perl code]

$CMD = join(' ', $cmd, $arg1, $arg2, $arg3);

Is there anything similar to this available in Ruby?

Thanks in advance.
Jagadeesh

17 Answers

Mohit Sindhwani

5/25/2009 5:58:00 AM

0

Jagadeesh wrote:
> Hi,
> I am looking for something similar to join in perl. I am doing
> [sample perl code]
>
> $CMD = join(' ', $cmd, $arg1, $arg2, $arg3);
>

Not 100% sure of what you need, but I think this will do the job for you:
str = [cmd, arg1, arg2, arg3].join(' ')


Stefano Crocco

5/25/2009 6:00:00 AM

0

On Monday 25 May 2009, Jagadeesh wrote:
> |Hi,
> |I am looking for something similar to join in perl. I am doing
> |[sample perl code]
> |
> |$CMD = join(' ', $cmd, $arg1, $arg2, $arg3);
> |
> |Is there anything similar to this available in Ruby?
> |
> |Thanks in advance.
> |Jagadeesh

I don't know perl, but, assuming you want a string containing the four
arguments separated by two spaces, you can do this:

res = [cmd, arg1, arg2, arg3].join(' ')

This creates an array containing the four strings, then calls its join method.

Stefano


Robert Klemme

5/25/2009 8:31:00 AM

0

2009/5/25 Mohit Sindhwani <mo_mail@onghu.com>:
> Jagadeesh wrote:
>>
>> Hi,
>> I am looking for something similar to join in perl. I am doing
>> [sample perl code]
>>
>> $CMD =3D join(' =A0', =A0$cmd, $arg1, $arg2, $arg3);
>>
>
> Not 100% sure of what you need, but I think this will do the job for you:
> str =3D [cmd, arg1, arg2, arg3].join(' ')

Alternative approaches:

str =3D "#{cmd} #{arg1} #{arg2} #{arg3}"
str =3D sprintf '%s %s %s %s', cmd, arg1, arg2, arg3

If this is for executing an external process, there is no need to lump
all these together, instead you can do which has the advantage that
you do not need a shell to parse the individual arguments and also
whitespace cannot cause trouble.

system cmd, arg1, arg2, arg3

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Mohit Sindhwani

5/25/2009 9:42:00 AM

0

Jagadeesh N. Malakannavar wrote:
> Hi Mohit,
>
> Thanks for super quick response. I am already using this way. I was
> not convinced by this method. I think there should be an API available
> for such thing. What you say?
There is a join method on the Array class that returns a String class.
In object-oriented terms, that makes perfect sense. Now, given your
specific needs, there are other ways to do that as others have
suggested. I don't think an extra API on the Kernel class makes sense
for stitching Strings together.

Cheers,
Mohit.
5/25/2009 | 5:42 PM.


Bertram Scharpf

5/25/2009 9:44:00 AM

0

Hi,

Am Montag, 25. Mai 2009, 17:30:43 +0900 schrieb Robert Klemme:
> 2009/5/25 Mohit Sindhwani <mo_mail@onghu.com>:
> > Jagadeesh wrote:
> >> [sample perl code]
> >> $CMD =3D join(' =A0', =A0$cmd, $arg1, $arg2, $arg3);
> >>
> > Not 100% sure of what you need, but I think this will do the job for yo=
u:
> > str =3D [cmd, arg1, arg2, arg3].join(' ')
>=20
> If this is for executing an external process, there is no need to lump
> all these together, instead you can do which has the advantage that
> you do not need a shell to parse the individual arguments and also
> whitespace cannot cause trouble.
>=20
> system cmd, arg1, arg2, arg3

There are two more advantages: Arguments that contain spaces
remain one argument. Arguments that contain shell operators like
; && || `...` could produce malicious side effects.

Another approach:

args =3D [ arg1, arg2, arg3]
system cmd, *args

Bertram


--=20
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Robert Klemme

5/25/2009 11:45:00 AM

0

2009/5/25 Bertram Scharpf <lists@bertram-scharpf.de>:
> Hi,
>
> Am Montag, 25. Mai 2009, 17:30:43 +0900 schrieb Robert Klemme:
>> 2009/5/25 Mohit Sindhwani <mo_mail@onghu.com>:
>> > Jagadeesh wrote:
>> >> [sample perl code]
>> >> $CMD =3D join(' =A0', =A0$cmd, $arg1, $arg2, $arg3);
>> >>
>> > Not 100% sure of what you need, but I think this will do the job for y=
ou:
>> > str =3D [cmd, arg1, arg2, arg3].join(' ')
>>
>> If this is for executing an external process, there is no need to lump
>> all these together, instead you can do which has the advantage that
>> you do not need a shell to parse the individual arguments and also
>> whitespace cannot cause trouble.
>>
>> system cmd, arg1, arg2, arg3
>
> There are two more advantages: Arguments that contain spaces
> remain one argument.

That's what I meant (see above).

> Arguments that contain shell operators like
> ; && || `...` could produce malicious side effects.

Hehe, true!

> Another approach:
>
> =A0args =3D [ arg1, arg2, arg3]
> =A0system cmd, *args

What advantage would it have to first create that array 'args'?

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestprac...

Bertram Scharpf

5/25/2009 2:47:00 PM

0

Hi,

Am Montag, 25. Mai 2009, 20:44:44 +0900 schrieb Robert Klemme:
> 2009/5/25 Bertram Scharpf <lists@bertram-scharpf.de>:
> > Another approach:
> >
> > =A0args =3D [ arg1, arg2, arg3]
> > =A0system cmd, *args
>=20
> What advantage would it have to first create that array 'args'?

I assumed that was a highly simplyfied example and argN stood for
"-f", "myfile", etc. Then the assignment were something like

=A0args =3D %w( -f myfile -i -c -q dummy)

Bertram


--=20
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Jagadeesh

5/25/2009 3:44:00 PM

0

On May 25, 4:44 pm, Robert Klemme <shortcut...@googlemail.com> wrote:
> 2009/5/25 Bertram Scharpf <li...@bertram-scharpf.de>:
>
>
>
> > Hi,
>
> > Am Montag, 25. Mai 2009, 17:30:43 +0900 schrieb Robert Klemme:
> >> 2009/5/25 Mohit Sindhwani <mo_m...@onghu.com>:
> >> > Jagadeesh wrote:
> >> >> [sample perl code]
> >> >> $CMD = join('  ',  $cmd, $arg1, $arg2, $arg3);
>
> >> > Not 100% sure of what you need, but I think this will do the job for you:
> >> > str = [cmd, arg1, arg2, arg3].join(' ')
>
> >> If this is for executing an external process, there is no need to lump
> >> all these together, instead you can do which has the advantage that
> >> you do not need a shell to parse the individual arguments and also
> >> whitespace cannot cause trouble.
>
> >> system cmd, arg1, arg2, arg3
>
> > There are two more advantages: Arguments that contain spaces
> > remain one argument.
>
> That's what I meant (see above).
>
> > Arguments that contain shell operators like
> > ; && || `...` could produce malicious side effects.
>
> Hehe, true!
>
> > Another approach:
>
> >  args = [ arg1, arg2, arg3]
> >  system cmd, *args
>
> What advantage would it have to first create that array 'args'?

Well this approach also look neat and readable.

Thanks


Rick DeNatale

5/25/2009 3:44:00 PM

0

On Mon, May 25, 2009 at 4:30 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:
>> Jagadeesh wrote:
>>>
>>> Hi,
>>> I am looking for something similar to join in perl. I am doing
>>> [sample perl code]
>>>
>>> $CMD =3D join(' =A0', =A0$cmd, $arg1, $arg2, $arg3);

> Alternative approaches:
>
> str =3D "#{cmd} #{arg1} #{arg2} #{arg3}"
> str =3D sprintf '%s %s %s %s', cmd, arg1, arg2, arg3
>
> If this is for executing an external process, there is no need to lump
> all these together, instead you can do which has the advantage that
> you do not need a shell to parse the individual arguments and also
> whitespace cannot cause trouble.
>
> system cmd, arg1, arg2, arg3

Well sometimes it's an advantage, but it's more of a difference
between a single and multiple string arguments to Kernel#system

If you WANT the shell to parse the cmd then you want a single string.
Of course you also need to be aware of the security aspects when you
use an unsanitized string coming from user input, rather than one
you've had more control over.

--=20
Rick DeNatale

Blog: http://talklikeaduck.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

Jagadeesh

5/25/2009 3:45:00 PM

0

On May 25, 2:42 pm, Mohit Sindhwani <mo_m...@onghu.com> wrote:
> Jagadeesh N. Malakannavar wrote:
> > Hi Mohit,
>
> > Thanks for super quick response. I am already using this way. I was
> > not convinced by this method. I think there should be an API available
> > for such thing. What you say?
>
> There is a join method on the Array class that returns a String class.  
> In object-oriented terms, that makes perfect sense.  Now, given your
> specific needs, there are other ways to do that as others have
> suggested.  I don't think an extra API on the Kernel class makes sense
> for stitching Strings together.

Hey Mohit, My intention was looking for better way of writing it. I
was not expecting *kernel* to have such an API.