I was writing the following piece of code:
class String
def title_case(exceptions = [])
exclude = %w(a an the some and but or not for) + exceptions
split(/\b/).map_with_index {|w, i|
(exclude.include?(w) or
((i>1) && self[i-1] == “’” && self[i-2] =~ /w/)) ? w : w.capitalize
}.join.capitalize
end
end
when I realised that ‘self’ was not what I wanted, since it belongs to
the block definition scope. What I want is a way to get at the anonymous
self.split object to which the block is passed - i.e. a variable recv,
analogous to self, that gets set when a block is yielded to or a proc is
called.
As it stands, I have to introduce a separate variable, b, just so I can
say b = split(/\b); b.map_with_index {|w,i| … b[i] …} - this
feels like it should be unnecessary. What would be nice would be for
’yield args’ to implicitly be ‘yield self, args’, proc.call(*args) be
proc.call(self, args) and {|args| …} be {|recv, args| }.
Good idea? Bad idea? Disastrously bad idea?
martin