[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

mixing variables and strings

arose

6/7/2006 9:54:00 PM

How to I mix variables and strings together.

In vb I use the & for example.

firstthree="abc"
firstsix=firstthree & "def"

5 Answers

Tim Hunter

6/7/2006 10:06:00 PM

0

arose wrote:
> How to I mix variables and strings together.
>
> In vb I use the & for example.
>
> firstthree="abc"
> firstsix=firstthree & "def"
>

firstthree="abc"
firstsix=firstthree + "def"

Andrew C.

6/8/2006 7:05:00 AM

0


"Tim Hunter" <cyclists@nc.rr.com> wrote in message
news:85Ihg.15702$Qg.7779@tornado.southeast.rr.com...
> arose wrote:
>> How to I mix variables and strings together.
>>
>> In vb I use the & for example.
>>
>> firstthree="abc"
>> firstsix=firstthree & "def"
>>
>
> firstthree="abc"
> firstsix=firstthree + "def"

Also, just for fun, try 'puts "123" * 3'. Initially, the result from this
struck me as odd, but in a good way.

Learning Ruby, I'm very much reminded of when I first learned C (which,
somewhat worryingly, is rapidly coming up for 20 years ago), and, compared
to what I already knew at the time (Pascal and various BASICs), C felt
slick, curious and sublime, not to mention, fun to work with.

A.


OliverMarchand

6/8/2006 7:11:00 AM

0


arose schrieb:
> How to I mix variables and strings together.
>
> In vb I use the & for example.
>
> firstthree="abc"
> firstsix=firstthree & "def"

For many String concatenations, do *not* use the + operator, rather
collect all the small strings to be concatenated in an array and use
..join("")

-----
n=10000

t = Time.new
str = ""
(0).upto(n) { str += "somestring"}
p Time.new-t


t = Time.new
str = []
(0).upto(n) { str.push("somestring") }
str = str.join("")
p Time.new-t
-----

yields

0.879806
0.038133

The reason is that whenever a string is appended via +, new space must
be made availabe in memory and that leads to subsequent copying of the
whole string. (This is what I assume, I have not looked at the ruby
source code...)

ciao,
Oliver

arose

6/8/2006 2:08:00 PM

0

thanks everybody


OliverMarchand wrote:
> arose schrieb:
> > How to I mix variables and strings together.
> >
> > In vb I use the & for example.
> >
> > firstthree="abc"
> > firstsix=firstthree & "def"
>
> For many String concatenations, do *not* use the + operator, rather
> collect all the small strings to be concatenated in an array and use
> .join("")
>
> -----
> n=10000
>
> t = Time.new
> str = ""
> (0).upto(n) { str += "somestring"}
> p Time.new-t
>
>
> t = Time.new
> str = []
> (0).upto(n) { str.push("somestring") }
> str = str.join("")
> p Time.new-t
> -----
>
> yields
>
> 0.879806
> 0.038133
>
> The reason is that whenever a string is appended via +, new space must
> be made availabe in memory and that leads to subsequent copying of the
> whole string. (This is what I assume, I have not looked at the ruby
> source code...)
>
> ciao,
> Oliver

Chris Hulan

6/8/2006 6:01:00 PM

0

You can also use substitution:

x='123' #=>'123'
y="#{x}ABC" #=> '123ABC'

Within double-quotes (") you can put code using #{code} and the result
of evaluating the code is put into the string.

Cheers