String concatenation in Ruby


#1

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


#2

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


#3

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


#4

2009/5/25 Mohit S. removed_email_address@domain.invalid:

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


#5

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


#6

Hi,

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

2009/5/25 Bertram S. removed_email_address@domain.invalid:

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


#7

2009/5/25 Bertram S. removed_email_address@domain.invalid:

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


#8

On May 25, 4:44 pm, Robert K. removed_email_address@domain.invalid 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


#9

On Mon, May 25, 2009 at 4:30 AM, Robert K.
removed_email_address@domain.invalid 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


#10

On May 25, 1:30 pm, Robert K. removed_email_address@domain.invalid 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


#11

On May 25, 7:47 pm, Bertram S. removed_email_address@domain.invalid 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


#12

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.


#13

On May 25, 2:42 pm, Mohit S. removed_email_address@domain.invalid 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.


#14

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


#15

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.


#16

On May 26, 11:00 pm, Brian C. removed_email_address@domain.invalid wrote:

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


#17

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.


#18

On May 26, 10:28 pm, Mohit S. removed_email_address@domain.invalid wrote:

HiJagadeesh,

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

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