Using context of method call

I am reading “The Ruby Programming Language” by David Flanagan, and Yukihiro Matsumoto — a piece about indexing a string. In Ruby to get a character from a string counting from the end of it you can use negative index, for example:

str[-5]

I am not the fan of such design, because in every language there is some valid range of indices and such approach doubles it. You have to be twice as much thorough in Ruby to make sure negative index is not a side effect of some computation. And negative numbers don’t come for free — it is one bit less for the digits.

And since just a day before I wished for having some reference to a caller when using call chaining altogether it gave me an idea — why not add to Skila simply a call context? Indexing a string could look like this:

str[\length-5]

Backslash (from top of my head) would switch context to caller, so any expression valid for it, would be valid in a call as well. Such feature could bring more flexibility to call chaining (I love this style except when I have to debug it).

I am not sure about it, but at least it is worth remembering, thus I posted it.

Tagged ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: