Forum: Ruby Join strings

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.
Fresh M. (Guest)
on 2009-04-10 15:36
What is the simples way to join strings?

tmp1 = 'aa'
tmp2 = 'bb'
tmp3 = nil
tmp4 = nil
tmp5 = 'dd'

result = 'aabbcc'
Leo (Guest)
on 2009-04-10 15:47
(Received via mailing list)
> result = 'aabbcc'

local_variables.select {|v| v != '_'}.map {|v| eval(v)}.join
=> "aabbdd"

Unfortunately, I don't know where you got the "cc" from in your
example.
Rick D. (Guest)
on 2009-04-10 15:52
(Received via mailing list)
On Fri, Apr 10, 2009 at 7:36 AM, Fresh M. <removed_email_address@domain.invalid> 
wrote:

> What is the simples way to join strings?
>
> tmp1 = 'aa'
> tmp2 = 'bb'
> tmp3 = nil
> tmp4 = nil
> tmp5 = 'dd'
>
> result = 'aabbcc'


[tmp1, tmp2, tmp3, tmp4, tmp5].join



--
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
Leo (Guest)
on 2009-04-10 15:52
(Received via mailing list)
> > result = 'aabbcc'

The problem & answer is almost the same as those to your question from
about a month ago:
http://groups.google.com/group/ruby-talk-google/br...
Jeff S. (Guest)
on 2009-04-10 16:40
(Received via mailing list)
Fresh M. wrote:
> What is the simples way to join strings?
>
> tmp1 = 'aa'
> tmp2 = 'bb'
> tmp3 = nil
> tmp4 = nil
> tmp5 = 'dd'
>
> result = 'aabbcc'

Possibly:

     "#{tmp1}#{tmp2}#{tmp3}#{tmp4}#{tmp5}"

If your needs are more complicated, maybe something like this:

     def join_tmp(indices, bind)
       eval "\"#{indices.collect {|i| "\#{tmp#{i}}" }.join}\"", bind
     end

     puts join_tmp(1..5, binding)
This topic is locked and can not be replied to.