Join strings


#1

What is the simples way to join strings?

tmp1 = ‘aa’
tmp2 = ‘bb’
tmp3 = nil
tmp4 = nil
tmp5 = ‘dd’

result = ‘aabbcc’


#2

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.


#3

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


#4

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/browse_frm/thread/3634f1422e7fb58d/f50828c615641397


#5

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)