I have recently started using Ruby, so quite new to this. My current objective is to use a ruby module called retort, my problem is that I don’t understand the configure method which is looking like this:
def configure
config = Config.new
yield config
@@service = XMLRPC::Client.new2(config.url)
end
Config class is simple and looks like:
class Config
attr_accessor :url
end
I tried to create a small example to play around in order to understand how exactly that is supposed to 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 it doesn’t return ‘test string’ but an empty string.
Thank you for any help 🙂
Can you be clearer about what’s confusing you? Does this code make sense to you?
The
yieldstatement invokes the block. The block returns a string, which is assigned to theconfigvariable back in thetest_methodand is then printed. Does that make it clearer?In your code, the line
yield configis invoking the block while passing in the just-instantiatedConfigobject. For instance: