From: "Hal E. Fulton" Date: 2001-05-05T12:23:48+09:00 Subject: [ruby-talk:14699] local/nonlocal block vars Well, I don't think I ever put in my (full) two cents... and frankly I got lost in the details when this was discussed before. This was one of the longest threads on the list, right up there with the "Ruby M*scot" discussion and "101 misconceptions about OO" or whatever that was. So forgive me here... while horse.dead? do horse.beat end Here's my take on this. I've numbered the points so that you can shoot them down more easily. :) 1. Sometimes we do want nonlocal block parameters. 2. Sometimes we don't. 3. Sometimes we do it accidentally. Oops! 4. IMO, the <> notation is ugly. 5. Somebody suggested a "with" notation which was not so bad. 6. There was talk about prefixes. Ehh. I like this somewhat... Here's how I would do it. 7. Use a Symbol constant to represent a variable from the outer scope. This has the advantages of the "prefix" idea without reusing a prefix like * or &. 8. If you have a Symbol, you use a variable from the outer scope. If not, it's local. 9. Disadvantage: Break old code? Waaahhh! No, I don't like this part either. 10. And here is the icing on the cake. Let me stir a little additional controversy by adding this rule: "If a Symbol appears *first* in a block parameter list, the variable in the outer scope springs into existence then." 11. Why this rule? No need to assign nil prior to the block. Also because "importing" and "exporting" variables from the block are somewhat separate issues. Continuing Johann's example: # obj and i are *not* defined here data_objects.each_with_index {|:obj, :i| if not obj.valid? break else obj.do_something end } # But now they are... if not obj.valid? print "The object at index ", i, " was invalid. Aborted." end ======== OK, that's probably at least two cents there. As Bill Shakespeare said, "Cry havoc, and let slip the dogs of war!" Hal