From: Albert Schlef on 11 Apr 2010 21:00 Let's say I have this code: class Mambo attr_accesstor :safe end Now, 'safe', in my case, is a boolean. Problem is, the getter will be named 'safe', whereas I want it to be 'safe?'. In other words, how do I make the getter 'safe?' and the setter 'safe' ? -- Posted via http://www.ruby-forum.com/.
From: Daniel N on 11 Apr 2010 21:03 [Note: parts of this message were removed to make it a legal post.] There's no built in way in ruby for it to know that you're going to put a boolean in there. You'll need to do something like class Mambo attr_writer :safe def safe? !!@safe end end HTH Daniel On 12 April 2010 11:00, Albert Schlef <albertschlef(a)gmail.com> wrote: > Let's say I have this code: > > class Mambo > attr_accesstor :safe > end > > Now, > > 'safe', in my case, is a boolean. Problem is, the getter will be named > 'safe', whereas I want it to be 'safe?'. > > In other words, how do I make the getter 'safe?' and the setter 'safe' > ? > -- > Posted via http://www.ruby-forum.com/. > >
From: Daniel N on 11 Apr 2010 21:13 [Note: parts of this message were removed to make it a legal post.] On 12 April 2010 11:10, Aaron Patterson <aaron(a)tenderlovemaking.com> wrote: > On Mon, Apr 12, 2010 at 10:00:06AM +0900, Albert Schlef wrote: > > Let's say I have this code: > > > > class Mambo > > attr_accesstor :safe > > end > > > > Now, > > > > 'safe', in my case, is a boolean. Problem is, the getter will be named > > 'safe', whereas I want it to be 'safe?'. > > > > In other words, how do I make the getter 'safe?' and the setter 'safe' > > ? > > In situations like this, I make two "getters" and leave the assignment > alone. Other ruby programmers expect your "setters" to be in the form > of "obj.safe = true", not "obj.safe(true)". > > Try something like this: > > class Mambo > attr_accesstor :safe > alias :safe? :safe > end > > Here it is in action: > > irb(main):001:0> class Mambo > irb(main):002:1> attr_accessor :safe > irb(main):003:1> alias :safe? :safe > irb(main):004:1> end > => nil > irb(main):005:0> m = Mambo.new > => #<Mambo:0x000001010cab50> > irb(main):006:0> m.safe = true > => true > irb(main):007:0> m.safe? > => true > irb(main):008:0> > > -- > Aaron Patterson > http://tenderlovemaking.com/ Unfortunately this doesn't support safe? being boolean always. It could be something other than true false (may not be a problem) If you want to use attr_accessor rather than simply aliasing the method I'd suggest def safe? !!safe end This way the expectation of a ? method to return a boolean is preserved. Cheers Daniel
From: Albert Schlef on 12 Apr 2010 01:28 Aaron Patterson wrote: > alias :safe? :safe Daniel wrote: > def safe? > !!@safe > end Thank you both. I wanted to make sure there's no built-in way to do this. (BTW, in Lisp (CLOS) it's possible to specify the getter/setter names.) -- Posted via http://www.ruby-forum.com/.
From: Albert Schlef on 12 Apr 2010 01:35 Albert Schlef wrote: > Thank you both. I wanted to make sure there's no built-in way to do > this. Hey, I've just discovered that 'ri' tells me that Module has an 'attr_reader?' method that creates a question-mark attribute. Problem is, 'ri' displays methods from everything I've installed on my system, so I can't know if 'attr_reader?' is provided by Ruby itself or by some wacky gem. -- Posted via http://www.ruby-forum.com/.
|
Next
|
Last
Pages: 1 2 3 Prev: [ANN] Harvested 0.3.0 Next: Google Wave: A new type of "Ruby Quiz" ? |