Are Ruby formatted strings and interpolated strings identical in behaviour? -
do following 2 lines of code behave in same way despite different implementations
values.map{ |k,v| __send__('%s=' % k.to_s, v) } values.map{ |k,v| __send__("#{k.to_s}=", v) }
the second line more common ruby idiom wondering why other method used when in rails core expect use idiomatic ruby.
they not absolutely identical. instance, first example call string#%
, if method redefined strange reason, might different result. standard definition of string#%
, strings computed same, both expressions have same result.
btw, there's no need to_s
in example, , assuming send
has not been redefined (and equivalent __send__
):
values.map{ |k,v| send("#{k}=", v) }
Comments
Post a Comment