Ruby yield newbie issue -
i have started using ruby, quite new this. current objective use ruby module called retort, problem don't understand configure method looking this:
def configure config = config.new yield config @@service = xmlrpc::client.new2(config.url) end
config class simple , looks like:
class config attr_accessor :url end
i tried create small example play around in order understand how supposed work:
class testclass def test_method config = string.new yield config p config end end d = testclass.new d.test_method { 'test string' }
of course doesn't return 'test string' empty string.
thank :)
can clearer what's confusing you? code make sense you?
class testclass def test_method config = yield p config end end d.test_method { "test string" }
the yield
statement invokes block. block returns string, assigned config
variable in test_method
, printed. make clearer?
in code, line yield config
invoking block while passing in just-instantiated config
object. instance:
def foo s = "a string" yield s p "in foo printing " + s end foo { |x| p "in block printing " + x }
Comments
Post a Comment