From: Rob Marshall Date: 2013-02-07T05:14:28+09:00 Subject: Re: Implementing DRY with a function call To Robert...yes, I have NOT, yet, grasped the concept of blocks :-) Let me explain: I'm using FFI to access a library. The way the library calls work is that none of them return errors or even raise exceptions. The expectation is, that after calling a function, the user will check (via a function call) for errors. So what I end up with is: Module MyModule class SubClass def initialize(instance_var) @instance_var = instance_var end def my_function_call(arg1,arg2) unless @instance_var.nil? ret = MyModule.Real_FunctionCall(@instance_var,arg1,arg2) if MyModule.CheckLastError != 0 raise MyException, MyModule.GetLastErrorText end return ret else raise ArgumentError, "Instance variable missing" end end def my_other_call(arg1,arg2, arg3) ret = MyModule.Real_OtherCall(arg1,arg2,arg3) if MyModule.CheckLastError != 0 raise MyException, MyModule.GetLastErrorText end return ret end end end The difference is that I have many classes in the module and LOTS of functions within each class. As you can see, in some cases I want to check an instance variable before making the call (because the the function call will need it), and in other cases I don't need to. But in ALL cases I need to do the error check. With close to a hundred+ functions, that's 300+ repetitious lines of code. What I think will work is (here's a simplified example): def check_error(*args) if args.length > 0 args.each_with_index { |v,i| printf "%2d argument value: %s\n", i, v } end ret = yield puts "Check error here" ret end check_error("this","is","a","test"){ return_value("something again") } 0 argument value: this 1 argument value: is 2 argument value: a 3 argument value: test Check error here => "This is the returning value: something again" check_error { return_value("something again") } Check error here => "This is the returning value: something again" The part I didn't understand is how Ruby will allow "you" to put a block after the args in parens...Once I figured that out (thanks to: http://blog.sidu.in/2007/11/ruby-blocks-gotchas.html#.URK0k-j6bYA) it all made sense :-) Thanks for the help, Rob -- Posted via http://www.ruby-forum.com/.