While reading Programming Ruby, I looked at this piece of code:
while gets num1, num2 = split /,/ end
While I intuitively understand what he is doing, I do not understand the syntax. "split" is a method in the String class - in Ruby, which string is the recipient of the "split" message in the above script?
I can see in the documents that βgetsβ assigns its result to the variable $ _, so I assume that it implicitly uses $ _ as the recipient, but a whole bunch of Google searches did not confirm this hunch. If so, I would like to know which general rule for methods is invoked without an explicit recipient.
I tried the code in irb, with some diagnostic calls added, and I confirmed that the actual behavior is what you expect - num1 and num2 get the assigned values ββthat were entered separated by a comma.
Bruce
source share