String concatenation in Ruby

J

Jagadeesh

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
 
M

Mohit Sindhwani

Jagadeesh said:
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(' ')
 
S

Stefano Crocco

|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
 
R

Robert Klemme

2009/5/25 Mohit Sindhwani said:
Jagadeesh said:
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.rubybestpractices.com/
 
M

Mohit Sindhwani

Jagadeesh said:
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.
 
B

Bertram Scharpf

Hi,

Am Montag, 25. Mai 2009, 17:30:43 +0900 schrieb Robert Klemme:
2009/5/25 Mohit Sindhwani said:
Jagadeesh said:
[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-scharpf.de
 
R

Robert Klemme

2009/5/25 Bertram Scharpf said:
Hi,

Am Montag, 25. Mai 2009, 17:30:43 +0900 schrieb Robert Klemme:
2009/5/25 Mohit Sindhwani said:
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.rubybestpractices.com/
 
B

Bertram Scharpf

Hi,

Am Montag, 25. Mai 2009, 20:44:44 +0900 schrieb Robert Klemme:
2009/5/25 Bertram Scharpf said:
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-scharpf.de
 
J

Jagadeesh

2009/5/25 Bertram Scharpf <[email protected]>:


Am Montag, 25. Mai 2009, 17:30:43 +0900 schrieb Robert Klemme:
2009/5/25 Mohit Sindhwani <[email protected]>:
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 foryou:
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
 
R

Rick DeNatale

Jagadeesh said:
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.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
J

Jagadeesh

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

Jagadeesh

2009/5/25 Mohit Sindhwani said:
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(' ')

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
 
J

Jagadeesh

Hi,

Am Montag, 25. Mai 2009, 20:44:44 +0900 schrieb Robert Klemme:
2009/5/25 Bertram Scharpf said:
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


Thank you all for your time.
Jagadeesh
 
M

Mohit Sindhwani

Jagadeesh said:
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.
 
B

Brian Candler

Mohit said:
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')
 
M

Mohit Sindhwani

Brian said:
Of course, it can have if you want:

def join(sep, *args)
args.join(sep)
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.
 
J

Jagadeesh

Of course, it can have if you want:

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

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

Superb!! Will start programming in Ruby now. :)
 

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
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top