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

S

Simon Schuster

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


neat!
 
D

Dan Zwell

Simon said:
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...
 
D

Dan Zwell

Axel said:
Hello,


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

Thank you!

Axel

var1 <an operator>= var2

is always equivalent to

var1 = var2 <an operator> 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
 
P

Peña, Botp

From: Dan Zwell [mailto:[email protected]]=20
# &&=3D is good for making sure a group of things are all true:
# result =3D true
# expressions.each {|expr| result &&=3D expr}

just in case you've forgotten, Dan,=20

expressions.all?


kind regards -botp
 
S

Sebastian Hungerecker

Dan said:
"+=" 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.
 
R

Robert Dober

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


neat!
not really :(
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

robert@PC:~/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)
robert@PC:~/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)
robert@PC:~/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)
robert@PC:~/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
 
D

David A. Black

Hi --

not really :(
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 :)
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 :)


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Robert Dober

Don't worry -- there's no Academie Anglaise :)
I am not French, only my wife is;), but it is a funny remark.
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 :)
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
 
D

David A. Black

Hi --

I am not French, only my wife is;), but it is a funny remark.
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

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
R

Robert Dober

Hi --



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

Dan Zwell

Peña said:
From: Dan Zwell [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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,316
Members
48,002
Latest member
DoloresMan

Latest Threads

Top