1 2 3 4 5
Even though the function specifically calls for an argument (
h), it can still be called without one. This construction has a ruby equivalent,
attr_accessor :headline, which behaves as a getter and a setter, depending on the arguments provided (none, or
headline = new_value).
This “variable-parameter” function design is what enables method chaining. When the function is called as a getter it returns a string, but when it is called as a setter it returns the invoking object, passing itself along to the next method:
1 2 3 4
Data Visualization with D3.js Cookbook (affiliate link)