How can I do this better?

S

Sam Kong

Hi,

I find myself to use the following idiom frequently.

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

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

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

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

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

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

How do you do it?

Sam
 
J

Jean Helou

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

jean
 
J

Jean Helou

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

limited testing in a trivial case in for both

jean

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

jean
Hi,

I find myself to use the following idiom frequently.

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

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

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

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

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

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

How do you do it?

Sam

 
S

Simon Strandgaard

I find myself to use the following idiom frequently.

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

I like the post-modifying if statement.
But I feel uncomfortable with calling @cache 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
 
R

Robert Klemme

I find myself to use the following idiom frequently.

def foo i
return @cache if @cache
@cache = 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-hashes.html


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
end

Cheers

robert
 
S

Sam Kong

Hi Robert,

Robert said:
(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
 
S

Sam Kong

Robert said:
I think so too, but just one worry, did you read me here?
Tthis was only a side remark but what I meant is

v = ... and return v # does what you want
v = ... && return v # is not even correct syntax

Cheers
R.

Of course, I read you.^^
That's a good suggestion.
@cache ||= some_method(i) kills all others, though.

Thanks.

Sam
 

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,222
Messages
2,571,138
Members
47,755
Latest member
Grazynkaa

Latest Threads

Top