[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Method Chaining Issues

aartist

6/1/2005 4:15:00 PM

try this:
string = "I am sold"
string.sub!('old','new').reverse!;
puts string

It works.

If string didn't contain the word 'old' then it will fail.
Is that a nice behavior?
Can it be changed to work on whatever input?

27 Answers

Ralph \"PJPizza\" Siegler

6/1/2005 4:22:00 PM

0

On Thu, Jun 02, 2005 at 01:15:22AM +0900, aartist wrote:
> try this:
> string = "I am sold"
> string.sub!('old','new').reverse!;
> puts string
>
> It works.
>
> If string didn't contain the word 'old' then it will fail.
> Is that a nice behavior?
> Can it be changed to work on whatever input?
>
>

Maybe this just shows that in-place modifying methods that can potentially return nil or an object without needed methods are too dangerous to chain for bullet-proof applications - in this case use .sub and .reverse with tests along the way


Thomas Counsell

6/1/2005 4:25:00 PM

0

Hello

On 1 Jun 2005, at 17:15, aartist wrote:
> try this:
> string = "I am sold"
> string.sub!('old','new').reverse!;
> puts string
>
> It works.
>
> If string didn't contain the word 'old' then it will fail.
> Is that a nice behavior?
> Can it be changed to work on whatever input?

Drop the !s

string = string.sub('old','mew').reverse

Seems fine to me!

Tom

--
http://tom.co...


Dan Fitzpatrick

6/1/2005 4:30:00 PM

0

aartist wrote:
> try this:
> string = "I am sold"
> string.sub!('old','new').reverse!;
> puts string
>
> It works.
>
> If string didn't contain the word 'old' then it will fail.
> Is that a nice behavior?
> Can it be changed to work on whatever input?
>
>
You can remove the nil case like this:

(string.sub!('x','new') || string).reverse!

Dan


Gavin Kistner

6/1/2005 4:34:00 PM

0

This is a FAQ, though no page on the RubyGarden wiki seems to address
it.

Some kind soul should search the archives for all the threads and RCRs
surrounding this topic and create a page on the wiki with links, so we
can redirect people to it.

In short - some people think it's nice that the 'bang' methods return
nil if no change was effected, some people do not. You can either chain
them using intermediary strings:

string.sub('old','new').reverse

or you can use the bang methods, but not use chaining

string.sub!('old','new')
string.reverse!

Nikolai Weibull

6/1/2005 5:12:00 PM

0

Phrogz wrote:

> Some kind soul should search the archives for all the threads and RCRs
> surrounding this topic and create a page on the wiki with links, so we
> can redirect people to it.
>
> In short - some people think it's nice that the 'bang' methods return
> nil if no change was effected, some people do not. You can either chain
> them using intermediary strings:
>
> string.sub('old','new').reverse
>
> or you can use the bang methods, but not use chaining
>
> string.sub!('old','new')
> string.reverse!

It should also be pointed out that the non-destructive version of sub
(i.e., String#sub) method often turns out to be faster than the
destructive one (i.e., String#sub!),
nikolai

--
Nikolai Weibull: now available free of charge at http:/...!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


Gyoung-Yoon Noh

6/1/2005 7:09:00 PM

0

On 6/2/05, Nikolai Weibull
<mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:
> It should also be pointed out that the non-destructive version of sub
> (i.e., String#sub) method often turns out to be faster than the
> destructive one (i.e., String#sub!),
> nikolai
>

No, what you said are inverted.
Some non-destructive methods are usually implemented by
simply copying reference after calling destructive method.
It looks like:

def sort(ary):
sort!(ary)
return ary
end

This is a benchmark results.

$ cat destructive.rb
require 'benchmark'

Benchmark.bmbm do |bm|
bm.report("Destructive") do
1000.times do
ary = (0..10000).to_a
ary.reverse!
ary.sort!
end
end
bm.report("Non-destructive") do
1000.times do
ary = (0..10000).to_a
ary.reverse.sort
end
end
end

$ ruby destructive.rb
Rehearsal ---------------------------------------------------
Destructive 2.910000 0.000000 2.910000 ( 2.904260)
Non-destructive 3.110000 0.000000 3.110000 ( 3.116915)
------------------------------------------ total: 6.020000sec

user system total real
Destructive 2.930000 0.000000 2.930000 ( 2.935663)
Non-destructive 3.170000 0.010000 3.180000 ( 3.171050)

--
http://nohmad.su...


Sam Goldman

6/1/2005 7:45:00 PM

0

Phrogz wrote:

>This is a FAQ, though no page on the RubyGarden wiki seems to address
>it.
>
>Some kind soul should search the archives for all the threads and RCRs
>surrounding this topic and create a page on the wiki with links, so we
>can redirect people to it.
>
>In short - some people think it's nice that the 'bang' methods return
>nil if no change was effected, some people do not. You can either chain
>them using intermediary strings:
>
> string.sub('old','new').reverse
>
>or you can use the bang methods, but not use chaining
>
> string.sub!('old','new')
> string.reverse!
>
>
>
Some people think that "bang" methods shouldn't exist at all!

- John Q. Haskell


Sam Goldman

6/1/2005 7:53:00 PM

0

Gyoung-Yoon Noh wrote:

>On 6/2/05, Nikolai Weibull
><mailing-lists.ruby-talk@rawuncut.elitemail.org> wrote:
>
>
>>It should also be pointed out that the non-destructive version of sub
>>(i.e., String#sub) method often turns out to be faster than the
>>destructive one (i.e., String#sub!),
>> nikolai
>>
>>
>>
>
>No, what you said are inverted.
>Some non-destructive methods are usually implemented by
>simply copying reference after calling destructive method.
>
<snip>

While everything you said is true, a "smart enough" compiler can do some
very interesting optimizations on whole programs if it can assume the
idempotence of methods. This is one of the most attractive points of
functional programming (that and bringing math back into programming).

- Sam


Nikolai Weibull

6/1/2005 10:04:00 PM

0

Sam Goldman wrote:

> Some people think that "bang" methods shouldn't exist at all!

Definitely,
nikolai

--
Nikolai Weibull: now available free of charge at http:/...!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


Kyle Heon

6/1/2005 10:10:00 PM

0

>> Some people think that "bang" methods shouldn't exist at all!

>Definitely,
> Nikolai

I'm new to Ruby so I when you refer to a "bang" method you mean something
like "chomp!"? And if so, why do some thing they shouldn't exist? Just
curious.

Kyle Heon
kheon@comcast.net