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.
Ead43bcb0202410540a6a7cee5955d46?d=identicon&s=25 Fresh Mix (giga)
on 2009-04-10 13:36
What is the simples way to join strings?

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

result = 'aabbcc'
87ef5d1e14b148eb596433bc17ffe690?d=identicon&s=25 Leo (Guest)
on 2009-04-10 13: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.
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-04-10 13:52
(Received via mailing list)
On Fri, Apr 10, 2009 at 7:36 AM, Fresh Mix <gigatavu@gmail.com> 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
87ef5d1e14b148eb596433bc17ffe690?d=identicon&s=25 Leo (Guest)
on 2009-04-10 13: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...
149379873fe2cb70e550c6bff8fedd0c?d=identicon&s=25 Jeff Schwab (Guest)
on 2009-04-10 14:40
(Received via mailing list)
Fresh Mix 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.