[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Join strings

Gi Ga

4/10/2009 11:37:00 AM

What is the simples way to join strings?

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

result = 'aabbcc'
--
Posted via http://www.ruby-....

4 Answers

-lim-

4/10/2009 11:46:00 AM

0

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

4/10/2009 11:52:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

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.denh...
Twitter: http://twitter.com/Ri...
WWR: http://www.workingwithrails.com/person/9021-ric...
LinkedIn: http://www.linkedin.com/in/ri...

-lim-

4/10/2009 11:52:00 AM

0

> > 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/f50828...

Jeff Schwab

4/10/2009 12:36:00 PM

0

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)