dubstep
October 19, 2011, 8:49pm
#1
I have the following :
hs = {}
hs.default = nil
hs[‘name3’] = command3
hs[‘name2’] = command2
hs[‘name1’] = command1
Where command* are methods that can fail and raise an exception. The
only way I know to deal with this is :
begin
hs[‘name3’] = command3
rescue
end
begin
hs[‘name2’] = command2
rescue
end
begin
hs[‘name1’] = command1
rescue
end
Is there a more compact/nicer of dealing with this. Because I have about
10-15 of these in a row in my code that I unfortunately can’t change to
anything better.
What I would really want is something that if an exception is raised to
skip that line and just continue with the next line.
kdorsel
October 19, 2011, 9:05pm
#2
rbx-head :001 > def throw
rbx-head :002?> throw “hey”
rbx-head :003?> end
=> #<Rubinius::CompiledMethod throw file=(irb)>
rbx-head :004 > throw rescue puts “rescued”
rescued
=> nil
kdorsel
October 19, 2011, 9:14pm
#3
On Wed, Oct 19, 2011 at 11:49 AM, Kassym D. [email protected]
wrote:
rescue
end
Is there a more compact/nicer of dealing with this. Because I have about
10-15 of these in a row
http://stackoverflow.com/questions/2624835/ruby-continue-a-loop-after-catching-an-exception
kdorsel
October 19, 2011, 10:06pm
#4
On 20/10/11 08:45, Kassym D. wrote:
C. Zona wrote in post #1027457:
http://stackoverflow.com/questions/2624835/ruby-continue-a-loop-after-catching-an-exception
Something like that, but I can’t use a loop. Each command and each hash
entry name is different and have to be hard coded.
Do they need to be hard coded? Can you configure it with a hash of name
to command mapping, and then simply iterate the hash with each_pair?
irb
ruby-1.9.2-p290 :001 > hs = {}
=> {}
ruby-1.9.2-p290 :002 > hs.default = nil
=> nil
ruby-1.9.2-p290 :003 > def command1; ‘command1_result’;end
=> nil
ruby-1.9.2-p290 :004 > def command2; ‘command2_result’;end
=> nil
ruby-1.9.2-p290 :005 > def command3; ‘command3_result’;end
=> nil
ruby-1.9.2-p290 :006 > assignments = {:name1=>:command1,
:name2=>:command2, :name3=>:command3}
=> {:name1=>:command1, :name2=>:command2, :name3=>:command3}
ruby-1.9.2-p290 :007 > assignments.each_pair {|k,v| hs[k] =
method(v).call}
=> {:name1=>:command1, :name2=>:command2, :name3=>:command3}
ruby-1.9.2-p290 :008 > hs
=> {:name1=>“command1_result”, :name2=>“command2_result”,
:name3=>“command3_result”}
Then you have your loop.
Sam
kdorsel
October 19, 2011, 9:45pm
#5
C. Zona wrote in post #1027457:
http://stackoverflow.com/questions/2624835/ruby-continue-a-loop-after-catching-an-exception
Something like that, but I can’t use a loop. Each command and each hash
entry name is different and have to be hard coded.
Posted by Steve K. (Guest) on 2011-10-19 21:05
What you are saying is just to add ‘rescue’ after each line. ie :
hs = {}
hs.default = nil
hs[‘name3’] = command3 rescue
hs[‘name2’] = command2 rescue
hs[‘name1’] = command1 rescue
Does not seem to work, neither does adding ‘rescue end’ to each line
kdorsel
October 19, 2011, 10:38pm
#6
That could work. Just one last thing to solve.
assignments = {:name1=>“arr.grep(/Input:.*/)[0][6…-1]”}
assignments.each_pair {|k,v| hs[k] = how do I call this command
associated with :name1}
.method(:method).call(arguments) and .send(“method”) only seem to work
for single methods and not combined ones like the example above.
kdorsel
October 19, 2011, 11:48pm
#7
On 20/10/11 09:38, Kassym D. wrote:
That could work. Just one last thing to solve.
assignments = {:name1=>“arr.grep(/Input:.*/)[0][6…-1]”}
assignments.each_pair {|k,v| hs[k] = how do I call this command
associated with :name1}
.method(:method).call(arguments) and .send(“method”) only seem to work
for single methods and not combined ones like the example above.
Well, you can do this, but remember you have to deal with exceptions
thrown in your proc.
ruby-1.9.2-p290 :001 > hs = {}
=> {}
ruby-1.9.2-p290 :002 > target = []
=> []
ruby-1.9.2-p290 :003 > assignments = {:name1=>Proc.new {|arr| foo =
arr.grep(/Input:.*/)[0]; foo && foo[6…-1] || nil}}
=> {:name1=>#Proc:[email protected] :3(irb) }
ruby-1.9.2-p290 :004 > assignments.each_pair {|k,v| hs[k] =
v.call(target)}
=> {:name1=>#Proc:[email protected] :3(irb) }
ruby-1.9.2-p290 :005 > hs
=> {:name1=>nil}
The proc in assignments could be something like this if you didn’t want
the foo (you can use tap or something to debug inside the proc if needs
be)
ruby-1.9.2-p290 :006 > assignments = {:name1=>Proc.new {|arr|
arr.grep(/Input:.*/)[0][6…-1] rescue nil}}
=> {:name1=>#Proc:[email protected] :6(irb) }
ruby-1.9.2-p290 :007 > assignments.each_pair {|k,v| hs[k] =
v.call(target)}
=> {:name1=>#Proc:[email protected] :6(irb) }
ruby-1.9.2-p290 :008 > hs
=> {:name1=>nil}
Does that help?
Sam
kdorsel
October 20, 2011, 1:41am
#8
On Oct 19, 2011, at 2:49 PM, Kassym D. wrote:
only way I know to deal with this is :
A couple options:
hs = {}
hs[‘name3’] = (command3 rescue nil)
hs[‘name2’] = (command2 rescue nil)
hs[‘name1’] = (command1 rescue nil)
Or how about:
module Capture
def capture(key)
self[key] = yield
rescue
self[key] = nil
end
end
hs = {}.extend Capture
hs.capture(‘name3’) { command3 }
hs.capture(‘name2’) { command2 }
hs.capture(‘name1’) { command1 }