[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

something I just found out, am sharing (:newbish

Simon Schuster

8/24/2007 4:11:00 AM

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


neat!

12 Answers

Dan Zwell

8/24/2007 5:04:00 AM

0

Simon Schuster 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...

Axel

8/24/2007 7:33:00 AM

0

Hello,

> "*=", "/=", "&&=", "||=", and any others that I may have missed...

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

Thank you!

Axel


Dan Zwell

8/24/2007 7:59:00 AM

0

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

Peña, Botp

8/24/2007 8:08:00 AM

0

From: Dan Zwell [mailto:dzwell@gmail.com]
# &&= 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

Sebastian Hungerecker

8/24/2007 8:12:00 AM

0

Dan Zwell 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.

Robert Dober

8/24/2007 8:19:00 AM

0

On 8/24/07, Simon Schuster <significants@gmail.com> wrote:
> 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
>
>


--
I'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn

Axel

8/24/2007 8:41:00 AM

0


> 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

David A. Black

8/24/2007 11:09:00 AM

0

Robert Dober

8/24/2007 11:30:00 AM

0

On 8/24/07, David A. Black <dblack@rubypal.com> wrote:
> Hi --

>
> Don't worry -- there's no Academie Anglaise :-)
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 :-)
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

--
I'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn

David A. Black

8/24/2007 11:38:00 AM

0