Forum: Ruby String concatenation in Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ad911dd48003ce79e04880d4f643e47f?d=identicon&s=25 Jagadeesh (Guest)
on 2009-05-25 07:56
(Received via mailing list)
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
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2009-05-25 07:58
(Received via mailing list)
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(' ')
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2009-05-25 08:00
(Received via mailing list)
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-05-25 10:31
(Received via mailing list)
2009/5/25 Mohit Sindhwani <mo_mail@onghu.com>:
> 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
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-05-25 11:45
(Received via mailing list)
Hi,

Am Montag, 25. Mai 2009, 17:30:43 +0900 schrieb Robert Klemme:
> 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-05-25 13:45
(Received via mailing list)
2009/5/25 Bertram Scharpf <lists@bertram-scharpf.de>:
>>
>> 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
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-05-25 16:50
(Received via mailing list)
Hi,

Am Montag, 25. Mai 2009, 20:44:44 +0900 schrieb Robert Klemme:
> 2009/5/25 Bertram Scharpf <lists@bertram-scharpf.de>:
> > 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
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-05-25 17:45
(Received via mailing list)
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 = 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
Ad911dd48003ce79e04880d4f643e47f?d=identicon&s=25 Jagadeesh (Guest)
on 2009-05-25 17:47
(Received via mailing list)
On May 25, 4:44 pm, Robert Klemme <shortcut...@googlemail.com> 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
Ad911dd48003ce79e04880d4f643e47f?d=identicon&s=25 Jagadeesh (Guest)
on 2009-05-25 17:54
(Received via mailing list)
On May 25, 7:47 pm, Bertram Scharpf <li...@bertram-scharpf.de> 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
Ad911dd48003ce79e04880d4f643e47f?d=identicon&s=25 Jagadeesh (Guest)
on 2009-05-25 17:55
(Received via mailing list)
On May 25, 1:30 pm, Robert Klemme <shortcut...@googlemail.com> 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
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2009-05-26 04:45
(Received via mailing list)
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.
Ad911dd48003ce79e04880d4f643e47f?d=identicon&s=25 Jagadeesh (Guest)
on 2009-05-26 10:49
(Received via mailing list)
On May 25, 2:42 pm, Mohit Sindhwani <mo_m...@onghu.com> 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.
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2009-05-26 19:29
(Received via mailing list)
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.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-05-26 20:00
Mohit Sindhwani 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')
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2009-05-26 20:07
(Received via mailing list)
Brian Candler 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.
Ad911dd48003ce79e04880d4f643e47f?d=identicon&s=25 Jagadeesh (Guest)
on 2009-05-27 17:46
(Received via mailing list)
On May 26, 10:28 pm, Mohit Sindhwani <mo_m...@onghu.com> wrote:
> HiJagadeesh,
>
> I know what you mean -
Take it easy. :(

 I was just aticulating why "join" wouldn't exist
Ad911dd48003ce79e04880d4f643e47f?d=identicon&s=25 Jagadeesh (Guest)
on 2009-05-27 17:46
(Received via mailing list)
On May 26, 11:00 pm, Brian Candler <b.cand...@pobox.com> wrote:
>   puts join(' ','hello','world')
Superb!! Will start programming in Ruby now. :)
This topic is locked and can not be replied to.