From: Yuh-Ruey Chen Date: 2008-11-02T07:38:49+09:00 Subject: Re: How to access to local variables in enclosing scopes? On Nov 1, 7:26 am, "David A. Black" wrote: > > I guess my big beef with Ruby is that it is too complex and bloated > > with features that often are inconsistent or lack symmetry with each > > other. It's too "magical", making it hard to learn exactly how the > > language works. From a language design perspective, Ruby really seems > > like a mess to me, although with moments of brilliance. Most of the > > time, when I'm metaprogramming, I'm guessing and looking at the > > output, hoping I got so-and-so quirk correct. > > I'd be interested in seeing some of these examples. In my experience, > a great deal of what people find confusing or opaque in Ruby turns out > to be confusing because it is so simple, and because there are so few > exceptions to the rules. I've always been impressed with the > strictness with which Ruby applies a small number of underlying > principles. It's possible that a bit of analysis of some examples > could help you nail some of those. Okay, there are several little quirks of Ruby that annoy me, but I'll just a list a couple: 1) When I saw the method! notation for mutation methods, I thought that was particularly brilliant. But then I noticed that methods are only postfixed with ! if there was already another method that did the same thing except on a new copy. It would've been nice of all mutation methods were postfixed with !. One use case for this would be trapping all mutation methods of a class for some purpose (e.g. making a read- only class, or a class that notifies an observer upon mutation). 2) Awkwardness of defining singleton methods. Contrast: class << obj def singleton_method 'foo' end end with: obj.singleton_method = function() { return 'blah' } Of course, YMMV with regards to "elegance". 3) There are public/protected/private modifiers for functions, but not for modules. (Yes, I'm aware that Ruby isn't Java, and that "private" means "instance-private" not "class-private".) 4) "Scoping" weirdness (or whatever it's called) with modules and classes. class A class << self attr_accessor :x def foo @x = 20 end end end A.foo p A.x B = Class.new B.instance_eval do attr_accessor :x def foo @x = 40 end end B.foo p B.x # error because self within B.instance_eval is still B, not the singleton thingamajig. Yet by some magic, |def foo| makes a singleton method. So there's really another type of "scope" or whatever it's called here: module vs. instance. And I have to remember not to trip up on it. Anyway, I can list other Ruby gripes, but this is all getting off topic.