From: Austin Ziegler Date: 2005-08-12T03:57:38+09:00 Subject: Re: Namespace of method arguments (Ruby 2 Idea?) On 8/11/05, rm_rails@cheapcomplexdevices.com wrote: > In the Programming Ruby book in the section of namespaces, I see the > example: > > require "trig" > require "action" > y = Trig.sin(Trig::PI/4) > wrongdoing = Action.sin(Action::VERY_BAD) > It seems very common to me to use constants from the module as > parameters to the module - and rather uncommon to want constants from > a different module as a parameer. > Is there a reason why a method's arguments can't automatically include > the namespace of the module the method came from so I could simply > write: > > y = Trig.sin(PI/4) > wrongdoing = Action.sin(VERY_BAD) > > and have it automatically look in the Trig namespace for the first PI > and the Action namespace for VERY_BAD? I think this would make a lot > of code that makes heavy use of constants look a lot cleaner; and > perhaps encourage people to use constants more often rather than just > magic numbers or strings or symbols as arguments. Um. 1. Symbols aren't "magic"; they're effectively constants. 2. What if I have: require 'action' module VERY_BAD Action.sin(VERY_BAD) end Obviously, this is, itself VERY_BAD if Action.sin does Action.sin Action::VERY_BAD instead of using ::VERY_BAD which is, in this case, self. Why? Because it means that if I *meant* to pass in ::VERY_BAD, I have to explicitly specify it rather than relying on Ruby's constant lookup rules. If Action is a *module*, then you have more flexibility: module Action def self.sin(my_sin) p my_sin end VERY_BAD = "I'm just drawn that way." end module VERY_BAD Action.sin(VERY_BAD) # => VERY_BAD end module VERY_BAD include Action Action.sin(VERY_BAD) # => "I'm just drawn that way." end It's probably better to leave this alone. -austin -- Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca