On May 19, 2006, at 10:26 AM, gwtmp01 / mac.com wrote: > > On May 19, 2006, at 10:03 AM, Austin Ziegler wrote: >> >> No, there isn't. Why would you want that? I see no advantage to it, >> and it's odd. I prefer this, though: >> >> class A >> attr_accessor :a, :b, :c >> private :b, :b= >> end > > Seems like a lot of redundancy there. You are typing :b three times. > What if you had three public attributes and three private? > > class A > attr_accessor :a, :b, :c, :d, :e, :f > private :d, :d=, :e, :e=, :f, :f= > end > > vs. > > class A > attr_accessor :a, :b, :c > private { attr_accessor :d, :e, :f } > end > > The hidden state behavior of public, private, protected (changing > the default visibility of method definitions) always seems a little > awkward to me. > When they are used with arguments it doesn't seem as awkward. > > > Gary Wright > > > > Here's an implementation. I almost laughed at myself when i figured out the right way to do it, trying to use all sorts of Binding.of_caller type magic to get the toggle methods to work properly, then I realized the correct way to do it. % cat block_private.rb class Module def private_body(&block) old_meths = instance_methods class_eval(&block) new_meths = instance_methods - old_meths new_meths.each do |meth| private(meth) end nil end end class A def pub_meth puts "This is fine" end private_body do def priv_meth puts "Can't call me." end end def pub_meth2 puts "Yay!" end end a = A.new a.pub_meth a.pub_meth2 a.priv_meth % ruby block_private.rb -:32: private method `priv_meth' called for #<A:0x1e82fc> (NoMethodError) This is fine Yay! The misordered output is an artifact of my shiny new script for doing examples.