String concatenation in Ruby

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

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(’ ')

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

2009/5/25 Mohit S. [email protected]:

str = [cmd, arg1, arg2, arg3].join(’ ')
Alternative approaches:

str = “#{cmd} #{arg1} #{arg2} #{arg3}”
str = 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

Hi,

Am Montag, 25. Mai 2009, 17:30:43 +0900 schrieb Robert K.:

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. Arguments that contain shell operators like
; && || ... could produce malicious side effects.

Another approach:

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

Bertram

Hi,

Am Montag, 25. Mai 2009, 20:44:44 +0900 schrieb Robert K.:

2009/5/25 Bertram S. [email protected]:

Another approach:

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

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

args = %w( -f myfile -i -c -q dummy)

Bertram

2009/5/25 Bertram S. [email protected]:

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’?

Kind regards

robert

On May 25, 4:44 pm, Robert K. [email protected] wrote:

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

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

On Mon, May 25, 2009 at 4:30 AM, Robert K.
[email protected] wrote:

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);

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.


Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

On May 25, 1:30 pm, Robert K. [email protected] wrote:

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

Alternative approaches:

str = “#{cmd} #{arg1} #{arg2} #{arg3}”
str = sprintf ‘%s %s %s %s’, cmd, arg1, arg2, arg3

Robert,
I love this approach. It makes my code more readable. Will use it.
Thanks

On May 25, 7:47 pm, Bertram S. [email protected] wrote:

What advantage would it have to first create that array ‘args’?
Stuttgart, Deutschland/Germanyhttp://www.bertram-scharpf.de
Thank you all for your time.
Jagadeesh

Jagadeesh wrote:

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.

Hi Jagadeesh,

I know what you mean - I was just aticulating why “join” wouldn’t exist
as something similar to the way perl has. That said, I guess sprintf
comes quite close!

Cheers,
Mohit.
5/27/2009 | 1:28 AM.

On May 25, 2:42 pm, Mohit S. [email protected] wrote:

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.

Mohit S. wrote:

I was just aticulating why “join” wouldn’t exist
as something similar to the way perl has.

Of course, it can have if you want:

def join(sep, *args)
args.join(sep)
end

puts join(’ ',‘hello’,‘world’)

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.

On May 26, 11:00 pm, Brian C. [email protected] wrote:

puts join(’ ',‘hello’,‘world’)
Superb!! Will start programming in Ruby now. :slight_smile:

Brian C. wrote:

end

puts join(’ ',‘hello’,‘world’)

oops… why it wouldn’t exist by default as something …
But i know what you mean.

Cheers,
Mohit.
5/27/2009 | 2:06 AM.

On May 26, 10:28 pm, Mohit S. [email protected] wrote:

HiJagadeesh,

I know what you mean -
Take it easy. :frowning:

I was just aticulating why “join” wouldn’t exist