[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How can I do this better?

Sam Kong

1/5/2007 9:01:00 AM

Hi,

I find myself to use the following idiom frequently.

def foo i
return @cache[i] if @cache[i]
@cache[i] = some_method_that_takes_long_time(i)
end

I like the post-modifying if statement.
But I feel uncomfortable with calling @cache[i] twice.

I wish this worked.
return v if v = @cache[i] #=> undefined local variable or method `v'

This works but it's long.
if v = @cache[i]
return v
end

Maybe this is a compromise.
if v = @cache[i] then return v end

I want "return statement" to stand out.
Post-modification looks better for that.
So I call @cache[i] twice.

How do you do it?

Sam

11 Answers

Jean Helou

1/5/2007 9:27:00 AM

0

def foo i
return @cache[i] ? @cache[i] : some_method_that_takes_long_time(i)
end

jean
On 1/5/07, Sam Kong <sam.s.kong@gmail.com> wrote:
> Hi,
>
> I find myself to use the following idiom frequently.
>
> def foo i
> return @cache[i] if @cache[i]
> @cache[i] = some_method_that_takes_long_time(i)
> end
>
> I like the post-modifying if statement.
> But I feel uncomfortable with calling @cache[i] twice.
>
> I wish this worked.
> return v if v = @cache[i] #=> undefined local variable or method `v'
>
> This works but it's long.
> if v = @cache[i]
> return v
> end
>
> Maybe this is a compromise.
> if v = @cache[i] then return v end
>
> I want "return statement" to stand out.
> Post-modification looks better for that.
> So I call @cache[i] twice.
>
> How do you do it?
>
> Sam
>
>
>

Jean Helou

1/5/2007 9:30:00 AM

0

actually this seems to work too :
def foo(1)
@cache[i]||=some_method_that_takes_long_time(i)
end

limited testing in a trivial case in for both

jean

On 1/5/07, Jean Helou <jean.helou@gmail.com> wrote:
> def foo i
> return @cache[i] ? @cache[i] : some_method_that_takes_long_time(i)
> end
>
> jean
> On 1/5/07, Sam Kong <sam.s.kong@gmail.com> wrote:
> > Hi,
> >
> > I find myself to use the following idiom frequently.
> >
> > def foo i
> > return @cache[i] if @cache[i]
> > @cache[i] = some_method_that_takes_long_time(i)
> > end
> >
> > I like the post-modifying if statement.
> > But I feel uncomfortable with calling @cache[i] twice.
> >
> > I wish this worked.
> > return v if v = @cache[i] #=> undefined local variable or method `v'
> >
> > This works but it's long.
> > if v = @cache[i]
> > return v
> > end
> >
> > Maybe this is a compromise.
> > if v = @cache[i] then return v end
> >
> > I want "return statement" to stand out.
> > Post-modification looks better for that.
> > So I call @cache[i] twice.
> >
> > How do you do it?
> >
> > Sam
> >
> >
> >
>

Robert Klemme

1/5/2007 10:00:00 AM

0

On 05.01.2007 10:30, Jean Helou wrote:
> actually this seems to work too :
> def foo(1)

You want "def foo(i)" here.

> @cache[i]||=some_method_that_takes_long_time(i)
> end

That's what I'd do. And then there is also memoize, which implements
this pattern:

http://raa.ruby-lang.org/projec...

Kind regards

robert

Simon Strandgaard

1/5/2007 10:02:00 AM

0

On 1/5/07, Sam Kong <sam.s.kong@gmail.com> wrote:
> I find myself to use the following idiom frequently.
>
> def foo i
> return @cache[i] if @cache[i]
> @cache[i] = some_method_that_takes_long_time(i)
> end
>
> I like the post-modifying if statement.
> But I feel uncomfortable with calling @cache[i] twice.
[snip]

how about?

irb(main):001:0> a = nil
=> nil
irb(main):002:0> a ||= 42
=> 42
irb(main):003:0> a ||= 666
=> 42
irb(main):004:0> a
=> 42


--
Simon Strandgaard
http://opc...

Andrew Stewart

1/5/2007 11:12:00 AM

0


On 5 Jan 2007, at 09:05, Sam Kong wrote:

> I find myself to use the following idiom frequently.
>
> def foo i
> return @cache[i] if @cache[i]
> @cache[i] = some_method_that_takes_long_time(i)
> end

You could also use the 'overlooked feature of Ruby hashes' and put
your lengthy method in the block you pass to Hash.new:

http://moonbase.rydia.net/mental/blog/programming/overlooke...
of-ruby-hashes.html

Regards,
Andy Stewart


Robert Klemme

1/5/2007 11:31:00 AM

0

On 05.01.2007 12:12, Andrew Stewart wrote:
>
> On 5 Jan 2007, at 09:05, Sam Kong wrote:
>
>> I find myself to use the following idiom frequently.
>>
>> def foo i
>> return @cache[i] if @cache[i]
>> @cache[i] = some_method_that_takes_long_time(i)
>> end
>
> You could also use the 'overlooked feature of Ruby hashes' and put your
> lengthy method in the block you pass to Hash.new:
>
> http://moonbase.rydia.net/mental/blog/programming/overlooked-feature-of-ruby-h...

That's definitively a better option - especially if i is non numeric. :-)

def initialize
@foo = Hash.new {|h,k| h[k] = some_method_that_takes_long_time(k)}
end

def foo(i)
@foo[i]
end

Cheers

robert

Gregory Seidman

1/5/2007 3:11:00 PM

0

On Fri, Jan 05, 2007 at 06:05:32PM +0900, Sam Kong wrote:
} Hi,
}
} I find myself to use the following idiom frequently.
}
} def foo i
} return @cache[i] if @cache[i]
} @cache[i] = some_method_that_takes_long_time(i)
} end
[...]
} How do you do it?

You may find this of use:

http://redcorundum.blogspot.com/2006/09/simple-flyweight-implementati...

} Sam
--Greg


Sam Kong

1/5/2007 4:31:00 PM

0

Hi gaurav,

gaurav bagga wrote:
> hi,
>
> def foo i
>
> @cache[i]||=some_method_that_takes_long_time(i)
>
> end

This is great.
I'll use this pattern from now on.

Thanks.

Sam


Sam Kong

1/5/2007 4:32:00 PM

0

Hi Gregory,

Gregory Seidman wrote:
> You may find this of use:
>
> http://redcorundum.blogspot.com/2006/09/simple-flyweight-implementati...

Yes.
It's very interesting and useful.

Thanks.

Sam


Sam Kong

1/5/2007 4:35:00 PM

0

Hi Robert,

Robert Dober wrote:
> (1) some people wondered recently why "or" and "and" have such low a
> precedence, I guess this example is a reason behind this.

Probably because ruby provides "||" and "&&" also for higher
precedence.

Thanks.
Sam