Something I just found out, am sharing (:newbish)

075:0> a = “hello”
“hello”
076:0> a += " there."
“hello there.”
077:0>

neat!

Simon S. wrote:

075:0> a = “hello”
“hello”
076:0> a += " there."
“hello there.”
077:0>

neat!

“+=” will actually work on any class that has “+” defined, including
custom classes. I assume it is the same with “-=”, “*=”, “/=”, “&&=”,
“||=”, and any others that I may have missed…

Hello,

“*=”, “/=”, “&&=”, “||=”, and any others that I may have missed…

Can anybody tell me, where to find, what this means?

Thank you!

Axel

Axel wrote:

Hello,

“*=”, “/=”, “&&=”, “||=”, and any others that I may have missed…

Can anybody tell me, where to find, what this means?

Thank you!

Axel

var1 = var2

is always equivalent to

var1 = var2 2

For example,

num ||= 0

is frequently used to mean “if num is nil, num = 0”. The longhand is
“num = num || 0”, as “num || 0” evaluates to num if num is non-nil, and
“0” otherwise. Good for making sure strings or numbers are initialized:
my_str ||= “”

&&= is good for making sure a group of things are all true:

result = true
expressions.each {|expr| result &&= expr}

The simpler operators are these:
a /= b
means:
a = a/b

Same goes for the others:
my_str = "hello "
my_str *= 3

is the same as:
my_str = my_str * 3

I no longer have my list of web sites I learned Ruby from, but it’s
pretty simple. Hope this helps.

Dan

From: Dan Z. [mailto:[email protected]]

&&= is good for making sure a group of things are all true:

result = true

expressions.each {|expr| result &&= expr}

just in case you’ve forgotten, Dan,

expressions.all?

kind regards -botp

Dan Z. wrote:

“+=” will actually work on any class that has “+” defined, including
custom classes. I assume it is the same with “-=”, “*=”, “/=”, “&&=”,
“||=”, and any others that I may have missed…

That is correct (you missed %=, |=, &=, ^=, <<=, >>=, **= and whatever
/I/
may have missed), except that in case of && and || it is not necessary
for
the class to have anthing defined, because those aren’t methods and they
work independently of the object’s class.

On 8/24/07, Simon S. [email protected] wrote:

075:0> a = “hello”
“hello”
076:0> a += " there."
“hello there.”
077:0>

neat!
not really :frowning:
a += " there" is
a = a + " there"

a << " there"

I might be accused of premature optimizationism (A word I just made
up, I have to make an ECR ;).
But seriously there is just no need to pay the cost of the
construction of another String / Array.
Performance difference is already enormous for moderately long strings :

511/11 > cat costs.rb

vim: sw=2 sts=2 ft=ruby expandtab tw=0:

require ‘benchmark’

N = ARGV.first.to_i
a = “Hello Nice Little "
Benchmark.bmbm do
|mybench|
mybench.report(” += “){ N.times{ a += “.” } }
mybench.report(” << "){ N.times{ a << “.” } }
end # Benchmark.bmbm do

[email protected]:~/log/ruby/ML 10:17:57
512/12 > ruby costs.rb 1000
Rehearsal ----------------------------------------
+= 0.000000 0.000000 0.000000 ( 0.002936)
<< 0.000000 0.000000 0.000000 ( 0.001008)
------------------------------- total: 0.000000sec

       user     system      total        real

+= 0.000000 0.010000 0.010000 ( 0.080035)
<< 0.000000 0.000000 0.000000 ( 0.000927)
[email protected]:~/log/ruby/ML 10:18:04
513/13 > ruby costs.rb 10000
Rehearsal ----------------------------------------
+= 0.160000 0.020000 0.180000 ( 0.565431)
<< 0.010000 0.000000 0.010000 ( 0.012393)
------------------------------- total: 0.190000sec

       user     system      total        real

+= 0.730000 0.050000 0.780000 ( 0.826307)
<< 0.010000 0.000000 0.010000 ( 0.013734)
[email protected]:~/log/ruby/ML 10:18:08
514/14 > ruby costs.rb 20000
Rehearsal ----------------------------------------
+= 0.620000 0.010000 0.630000 ( 0.741340)
<< 0.030000 0.000000 0.030000 ( 0.077221)
------------------------------- total: 0.660000sec

       user     system      total        real

+= 3.060000 0.000000 3.060000 ( 3.131026)
<< 0.030000 0.000000 0.030000 ( 0.076420)
[email protected]:~/log/ruby/ML 10:18:20
515/15 > ruby costs.rb 30000
Rehearsal ----------------------------------------
+= 1.380000 0.020000 1.400000 ( 1.452444)
<< 0.040000 0.000000 0.040000 ( 0.087939)
------------------------------- total: 1.440000sec

       user     system      total        real

+= 6.760000 0.040000 6.800000 ( 6.848208)
<< 0.040000 0.000000 0.040000 ( 0.036871)

Cheers
Robert

I no longer have my list of web sites I learned Ruby from, but it’s
pretty simple. Hope this helps.

Dan

Very nice! Thank you!

  • Axel

Hi –

On Fri, 24 Aug 2007, Robert D. wrote:

a += " there" is
a = a + " there"

a << " there"

I might be accused of premature optimizationism (A word I just made
up, I have to make an ECR ;).

Don’t worry – there’s no Academie Anglaise :slight_smile:

But seriously there is just no need to pay the cost of the
construction of another String / Array.
Performance difference is already enormous for moderately long strings :

It depends, though; sometimes you might want a copy, sometimes you
might definitely not. So the + vs. << decision may not be premature,
and the right decision may not even be the optimizing one :slight_smile:

David

Hi –

On Fri, 24 Aug 2007, Robert D. wrote:

It depends, though; sometimes you might want a copy, sometimes you
might definitely not. So the + vs. << decision may not be premature,
and the right decision may not even be the optimizing one :slight_smile:
Hmm David when one does
a += b
the copy is thrown away and you have the same semantics (save some
implicit calls to to_s) as for
a << b

They’re not interchangeable, though:

a = “Hello”
b = " there"
c = a

a += b
puts a # Hello there
puts c # Hello

a = “Hello”
c = a
a << b
puts a # Hello there
puts c # Hello there

David

On 8/24/07, David A. Black [email protected] wrote:

implicit calls to to_s) as for
puts c # Hello

a = “Hello”
c = a
a << b
puts a # Hello there
puts c # Hello there
That is indeed an important - probably the most important - point to
make, I believe that people are not always aware that a+=b is a = a +
b.

I have to admit that I assumed that OP wanted the a << b behavior, but
maybe he did not, that was quite a bad performance of mine…
Robert

On 8/24/07, David A. Black [email protected] wrote:

Hi –

Don’t worry – there’s no Academie Anglaise :slight_smile:
I am not French, only my wife is;), but it is a funny remark.

But seriously there is just no need to pay the cost of the
construction of another String / Array.
Performance difference is already enormous for moderately long strings :

It depends, though; sometimes you might want a copy, sometimes you
might definitely not. So the + vs. << decision may not be premature,
and the right decision may not even be the optimizing one :slight_smile:
Hmm David when one does
a += b
the copy is thrown away and you have the same semantics (save some
implicit calls to to_s) as for
a << b
My benchmarks show some dramatic performance degradation already but
imagine the GC kicking in because of the temporary copies, no,
honestly I feel that beginners
should be warned about that idiom.

For sure sometimes you want
a = b + c
but that is a different story.

Robert

Peña wrote:

kind regards -botp

Peña,
Good point–my example wasn’t very good idiomatic Ruby. However, there
are still good occasions to use &&= when your expressions are not in a
convenient list. Example: I have a backup script written in Ruby that
runs several system commands. I just don’t want to put all the commands
in a list. So I use &&= to keep track of the state of success of the
whole script.

Dan