T
Trans
Sometimes i do this:
return x if x
Anyway to code it as:
return_on x
t.
return x if x
Anyway to code it as:
return_on x
t.
Trans said:Sometimes i do this:
return x if x
Anyway to code it as:
return_on x
Sometimes i do this:
return x if x
Anyway to code it as:
return_on x
Sometimes i do this:
return x if x
Anyway to code it as:
return_on x
t.
Trans said:Sometimes i do this:
return x if x
Anyway to code it as:
return_on x
Simon said:I cannot recall I ever have used return, in this way,
but my coding style may be 5% different than yours.
Maybe I can improve my minimal insight.
Any good (larger) examples of this thing?
Trans said:funny thing i just came across a similar case for break. how would you
DRY this up and get rid of 'result'?
result = nil
files.each do
result = require file
break if result
end
if result
...
Trans said:I don't really have any examples that are repleat with it, but as to
insight it's especially convenient when caching a return value:
def x
return_on @cache[:x]
# do stuff
@cache[:x] = result_of_stuff
end
this gets rid of an extraneous variable too b/c otherwise, the more
efficient impl. is:
def x
r = @cache[:x]
return r if r
# do stuff
@cache[:x] = result_of_stuff
end
funny thing i just came across a similar case for break. how would you
DRY this up and get rid of 'result'?
result = nil
files.each do
result = require file
break if result
end
if result
...
Devin said:Well, this is much easier:
if files.any? {|file| require file }
...
Pit said:Trans said:I don't really have any examples that are repleat with it, but as to
insight it's especially convenient when caching a return value:
def x
return_on @cache[:x]
# do stuff
@cache[:x] = result_of_stuff
end
this gets rid of an extraneous variable too b/c otherwise, the more
efficient impl. is:
def x
r = @cache[:x]
return r if r
# do stuff
@cache[:x] = result_of_stuff
end
You could also implement this as
def x
@cache[:x] ||= (
# do stuff
result_of_stuff
)
end
funny thing i just came across a similar case for break. how would you
DRY this up and get rid of 'result'?
result = nil
files.each do
result = require file
break if result
end
if result
...
Joel said:This only helps a little...
result = files.each do |file|
r = require file and break r
end
Daniel said:I have made the most hackish thing to accomplish this. It is basically
a regexp-supporting preprocessor. I don't recommend using it.
#! /usr/bin/ruby -w
def preprocessor_define(*args, &blk)
# Determine line of caller
callerLine = caller.join.match(/[0-9]+/)[0].to_i
# Get the source of the running file
source = IO.read(__FILE__)
# Remove the callerLine line
source = source.split("\n")
source.delete_at(callerLine - 1)
source = source.join("\n")
# Do the replacement
source.gsub!(*args, &blk)
# Avoid function redefinition warnings
newName = ""
50.times {newName << (rand(26) + 65).chr }
source.gsub!("preprocessor_define", newName)
# Run the replacement
#puts source
eval source
exit
end
preprocessor_define(/return_on (.*)/) {|s| "return #{s[1]} unless
#{s[1]}.nil?"}
def test
zulu = 5
return_on zulu
puts "Broken."
end
test
Output: None, as 'puts Broken' never gets executed.
def x
return_on @cache[:x]
# do stuff
@cache[:x] = result_of_stuff
end
Gavin said:def x
return_on @cache[:x]
# do stuff
@cache[:x] = result_of_stuff
end
def x
@cache[:x] ||= begin
# do stuff
end
end
Joel said:Gavin said:def x
return_on @cache[:x]
# do stuff
@cache[:x] = result_of_stuff
end
def x
@cache[:x] ||= begin
# do stuff
end
end
Or (saving an exception handler setup):
def reverse x
@cache[:x] ||= (
puts "CACHING"
x.reverse
)
end
@cache = {}
p reverse("foo")
p reverse("foo")
__END__
Output:
CACHING
"oof"
"oof"
William said:Joel said:Or (saving an exception handler setup):Gavin said:def x
return_on @cache[:x]
# do stuff
@cache[:x] = result_of_stuff
end
def x
@cache[:x] ||= begin
# do stuff
end
end
def reverse x
@cache[:x] ||= (
puts "CACHING"
x.reverse
)
end
@cache = {}
p reverse("foo")
p reverse("foo")
__END__
Output:
CACHING
"oof"
"oof"
It doesn't work correctly.
def reverse x
@cache[:x] ||= (
puts "CACHING"
x.reverse
)
end
@cache = {}
p reverse("foo")
p reverse("what?")
p reverse("And this is cached, you think???")
--- output -----
CACHING
"oof"
"oof"
"oof"
Joel said:Gavin said:def x
@cache[:x] ||= begin
# do stuff
end
end
Or (saving an exception handler setup):
Devin said:Joel said:Gavin said:def x
@cache[:x] ||= begin
# do stuff
end
end
Or (saving an exception handler setup):
Does begin...end really have some sort of overhead? I've found that one
the nicest looking yet, and I'd hate to dismiss it based on a false
perception.
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.