[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ruby-dev summary 28206-28273

Minero Aoki

2/2/2006 9:38:00 AM

9 Answers

Martin DeMello

2/2/2006 2:41:00 PM

0

Minero Aoki <aamine@loveruby.net> wrote:
>
> [ruby-dev:28217] ANDCALL operator
>
> Nobuyoshi Nakada suggested a new operator `&?' (this notation is temporary)
> which evaluates left-hand-side expression, and if it is true then call
> right-hand-side method. For example:
>
> if a[1] and a[1].strip.empty?
> ||
> if a[1] &? strip.empty?
>
> h["key"] and h["key"].dispatch
> ||
> h["key"] &? dispatch
>
> The motivation of this operator is to avoid duplication of expression.
>
> Takaaki Tateishi proposed another idea, Object#nil? with block
> (again, this name is temporary).
>
> a[1].nil? {|str| str.strip.empty? }
> h["key"].nil? {|h| h.dispatch }
>
> This issue is still open.

Syntactically, a method might look better than an operator:

a[1].if?.strip.empty?
a[1].maybe.strip.empty?
a[1].and.strip.empty?

martin

Ara.T.Howard

2/2/2006 3:36:00 PM

0

Luc Heinrich

2/2/2006 4:20:00 PM

0

On 2 févr. 06, at 15:43, Martin DeMello wrote:

> a[1].if?.strip.empty?
> a[1].maybe.strip.empty?
> a[1].and.strip.empty?

How about replacing '.' by something else in those cases ? Something
with a slightly different semantic, like "send message only if the
receiver is not nil".

For example, using ':'

if a[1] and a[1].strip.empty? ==> if a[1]:strip.empty?
h["key"] and h["key"].dispatch ==> h['key']:dispatch

(I've tried with various other characters, ':' is the one which looks
the best imho)

--
Luc Heinrich - luc@honk-honk.com - http://www.hon...




MenTaLguY

2/2/2006 5:41:00 PM

0

Quoting Austin Ziegler <halostatue@gmail.com>:

> Worse, the use of #nil? would mean that anyone
> who has overridden #nil? would have broken behaviour

Out of curiousity, has anyone on this list ever overriden #nil? ?
If so, why?

-mental


Ara.T.Howard

2/2/2006 6:00:00 PM

0

Berger, Daniel

2/2/2006 8:17:00 PM

0

ara.t.howard@noaa.gov wrote:
> On Thu, 2 Feb 2006, Minero Aoki wrote:
>
>> [ruby-dev:28217] ANDCALL operator
>>
>> Nobuyoshi Nakada suggested a new operator `&?' (this notation is
>> temporary)
>> which evaluates left-hand-side expression, and if it is true then call
>> right-hand-side method. For example:
>>
>> if a[1] and a[1].strip.empty?
>> ||
>> if a[1] &? strip.empty?
>>
>> h["key"] and h["key"].dispatch
>> ||
>> h["key"] &? dispatch
>>
>> The motivation of this operator is to avoid duplication of expression.
>>
>> Takaaki Tateishi proposed another idea, Object#nil? with block
>> (again, this name is temporary).
>>
>> a[1].nil? {|str| str.strip.empty? }
>> h["key"].nil? {|h| h.dispatch }
>>
>> This issue is still open.
>
>
>
> suggestion:
>
>
> harp:~ > cat a.rb
> #
> # predicate methods to avoid double evaluation of expr
> #
> module Kernel
> def iff?(a, &b) a.instance_eval &b if a end
> alias_method "if?", "iff?"
> def iff!(a, &b) a.instance_eval &b unless a end
> alias_method "if!", "iff!"
> end
>
> a = [ 42 ]
> b = [ false ]
>
> #
> # if, and only if
> #
>
> iff?(a.first){ p self }
>
> #
> # if, and only if not
> #
>
> iff!(b.first){ p self }
>
>
>
> harp:~ > ruby a.rb
> 42
> false
>
>
> kind regards.
>
> -a

(Reposted to ruby-talk)

Nice. If only "if" were a Kernel method that we could tie it to back to
"if/else" constructs somehow:

iff a.first
...
else
...
end

Cue the Smalltalk weenies...

Regards,

Dan




Berger, Daniel

2/2/2006 8:57:00 PM

0

ara.t.howard@noaa.gov wrote:

<snip>

> suggestion:
>
>
> harp:~ > cat a.rb
> #
> # predicate methods to avoid double evaluation of expr
> #
> module Kernel
> def iff?(a, &b) a.instance_eval &b if a end
> alias_method "if?", "iff?"
> def iff!(a, &b) a.instance_eval &b unless a end
> alias_method "if!", "iff!"
> end

<snip>

a = nil

iff?(a.length > 2){ puts "yep" }

undefined method `length' for nil:NilClass (NoMethodError)

I guess we need delayed evaluation. Would Nobu's UDelegator help here?

http://tinyurl...

Regards,

Dan



Joel VanderWerf

2/2/2006 10:24:00 PM

0

Minero Aoki wrote:
> h["key"] &? dispatch

What about

h["key"].?dispatch

It's more visually similar to the ordinary method call

h["key"].dispatch

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Carlos

2/4/2006 11:49:00 AM

0

[Joel VanderWerf <vjoel@path.berkeley.edu>, 2006-02-02 23.23 CET]
> Minero Aoki wrote:
> > h["key"] &? dispatch
>
> What about
>
> h["key"].?dispatch
>
> It's more visually similar to the ordinary method call
>
> h["key"].dispatch


Or &. ?
k["key"]&.chomp!&.dispatch
--