The difference between s=s+�a� ans s<< �a�

Hi,

I wonder what is the difference between s=s+�a� ans s<< �a�?

Somebody told me that s<< �a� concatenates the string without mutation.
Is that right?

Thanks,
Alan

On Jun 21, 2006, at 3:15 PM, Alan M. wrote:


Posted via http://www.ruby-forum.com/.

Someone told you wrong.

s = s + “a” is concatenate s and “a” and return a new string, bind it
to the variable a.

s << “a” is append “a” to the string referenced by the variable s. It
changes s in place.

Of course, this is exactly the kind of question to be answered by ri.

ri String#<<
ri String#+

On 6/21/06, Alan M. [email protected] wrote:

Hi,

I wonder what is the difference between s=s+“a” ans s<< “a”?

s = s + “a”
(1) compute a new String object “s” + a
(2) set s as a reference to the newly created object

s<<“a”

(1) append “a” to the String object s refers to.

The latter is much faster and should be prefered when possible (it will
not
be possible if s is frozen e.g.)

Cheers
Robert

Somebody told me that s<< “a” concatenates the string without mutation.

Is that right?

Thanks,
Alan


Posted via http://www.ruby-forum.com/.


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

2006/6/21, Simon Kröger [email protected]:

I think
s = s + “a” #
or
s += “a”

should be the norm as long as you do not know that using it will cause
you problems (really only performance problems come to my mind)

I use the rule exactly the other way round: usually use << unless that
might cause problems. There’s a lot of code out there that does +=
while all it wants to do is appending. += is less efficient and with
my C++ background << more looks like “append”.

end

hello = ‘hello’
greet_matz hello
greet_nobu hello

#=> hello matz!
#=> hello matz! nobu!

you will get what you expect if you use +=

It depends on what you expect. If methods are expected to get
something that they append stuff to then the observed behavior is
exactly what you expect and want. :slight_smile:

For example, if you write a method that should append info about the
current instance to something then you should use << because then even
an IO object can be passed and the code still works as expected.

As generally, you should know your tools and be knowing what you do. :slight_smile:

Kind regards

robert

On 6/21/06, Simon Kröger [email protected] wrote:

(1) compute a new String object “s” + a
I think
s = s + “a” #
or
s += “a”

should be the norm as long as you do not know that using it will cause
you problems (really only performance problems come to my mind)

s << “a”
is just what you should do instead
of
s = s+ “a”
unless of course you get paid for GC :wink:

Have a look at this:


def greet_matz greeting
greeting << ’ matz!’
puts greeting
end

Calling writer methods on parameters is asking for side effects, that
might
be good or bad, I do not want to discuss that right now, but has
nothing
to do with the question of the OP.

def greet_nobu greeting

you will get what you expect if you use +=

Forgive me for being blunt: I will get what I expect if I use <<, I
might
not get what you expected.

cheers

Simon

Fortunately I did not state my favorite way to use puts
puts some_variable * 1 << “some text” << …
:wink:

Cheers
Robert


Deux choses sont infinies : l’univers et la bêtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

Robert D. wrote:

s<<“a”

(1) append “a” to the String object s refers to.

The latter is much faster and should be prefered when possible (it will not
be possible if s is frozen e.g.)

I think
s = s + “a” #
or
s += “a”

should be the norm as long as you do not know that using it will cause
you problems (really only performance problems come to my mind)

Have a look at this:

def greet_matz greeting
greeting << ’ matz!’
puts greeting
end

def greet_nobu greeting
greeting << ’ nobu!’
puts greeting
end

hello = ‘hello’
greet_matz hello
greet_nobu hello

#=> hello matz!
#=> hello matz! nobu!

you will get what you expect if you use +=

cheers

Simon

Most readable + safe for me is e.g.:

puts “Hello, #{name}!”

puts “your calculation: #{arg1} * #{arg2} = #{arg1 * arg2}”

Robert D. schrieb:

will cause

you problems (really only performance problems come to my mind)

I use the rule exactly the other way round: usually use << unless that
might cause problems. There’s a lot of code out there that does +=
while all it wants to do is appending. += is less efficient and with
my C++ background << more looks like “append”.

Well, if you realy know what you do and if ‘that might cause problems’
there is of course no problem with <<. ‘+= is less efficient’ smells
like
premature optimization.

you will get what you expect if you use +=

It depends on what you expect. If methods are expected to get
something that they append stuff to then the observed behavior is
exactly what you expect and want. :slight_smile:

Hmm, I (but that may be just me) wouldn’t write a method that ‘get
something that they append stuff to’. I would write a method which will
return the stuff and let the caller append it to whatever he wishes.
(I like it a bit functional style and do not like side effects)

For example, if you write a method that should append info about the
current instance to something then you should use << because then even
an IO object can be passed and the code still works as expected.

Yes, if I would write such a method, yes. If appending is exactly the
operation I want, I use it. sure.

As generally, you should know your tools and be knowing what
you do. :slight_smile:

That sums it up quite perfect.

Kind regards

robert

cheers

Simon

From: Pete [mailto:[email protected]]
Sent: Wednesday, June 21, 2006 11:45 PM

Most readable + safe for me is e.g.:

puts “Hello, #{name}!”

puts “your calculation: #{arg1} * #{arg2} = #{arg1 * arg2}”

In case this wasn’t obvious: this was an example.

As there is no syntactical difference between pass by value
and pass by reference in ruby I try to avoid modifying
parameters and would use += in favour of <<. (at least in such
cases)

cheers

Simon

p.s.: oh well, I know there is no such thing as pass by value
in ruby at all, it just feels like it for immediate values.

From: Robert D. [mailto:[email protected]]
Sent: Wednesday, June 21, 2006 11:10 PM

s << “a”
is just what you should do instead
of
s = s+ “a”
unless of course you get paid for GC :wink:

I think that should be solved in the interpreter not by the
programmer. Meanwhile you are right, of course << is more
efficient.

Calling writer methods on parameters is asking for side
effects, that might

I guess that was my whole point.

be good or bad, I do not want to discuss that right now, but
has nothing
to do with the question of the OP.

That might be true.

Fortunately I did not state my favorite way to use puts
puts some_variable * 1 << “some text” << …
:wink:

:sunglasses:

cheers

Simon