Nuby question: question marks in method names

in the course of looking for answers, seems like ? is part of the
method, ie, 'something?'
and this is the preferred way saying that something is returning a
boolean value.

Correct. You can also specify ! as part of the method name, and also =,
but this is a bit special.

1) is there any way to do this automatically?

Not at the moment, but it's easy to implement on your own:

class Module
  def attr_bool sym
    attr_reader "#{sym}?"
    attr_writer sym
  end
end

class MyClass
  attr_bool :foo
end

p MyClass.instance_methods - Class.methods

Mehr, Assaph (Assaph) wrote:

1) is there any way to do this automatically?
   
Not at the moment, but it's easy to implement on your own:

class Module
def attr_bool sym
   attr_reader "#{sym}?"
   attr_writer sym
end
end

class MyClass
attr_bool :foo
end

p MyClass.instance_methods - Class.methods

This won't work though

x = MyClass.new
x.foo = true
p x.foo? #=> nil

The problem seems to be that the writer is setting the variable @foo but the reader is reading the variable @foo?

···

--
Mark Sparshatt

Hi,

At Mon, 30 Aug 2004 17:46:18 +0900,
mark sparshatt wrote in [ruby-talk:110940]:

x = MyClass.new
x.foo = true
p x.foo? #=> nil

The problem seems to be that the writer is setting the variable @foo but
the reader is reading the variable @foo?

class Module
  def attr_bool sym
    attr_accessor sym
    alias_method "#{sym}?", sym
    remove_method sym
  end
end

···

--
Nobu Nakada

Excerpts from nobu.nokada@softhome.net's mail of 30 Aug 2004 (EDT):

class Module
  def attr_bool sym
    attr_accessor sym
    alias_method "#{sym}?", sym
    remove_method sym
  end
end

It's a minor issue, but I think this is more consistent with the
attr_accessor behavior:

class Module
  def bool_accessor *syms
    attr_accessor *syms
    syms.each { |sym| alias_method "#{sym}?", sym }
    remove_method *syms
  end
end

···

--
William <wmorgan-ruby-talk@masanjin.net>

thank yopu all for the replies. these gave me more insight
into how things in ruby are done.

···

On Thu, 2 Sep 2004 10:43:45 +0900, William Morgan <wmorgan-ruby-talk@masanjin.net> wrote:

Excerpts from nobu.nokada@softhome.net's mail of 30 Aug 2004 (EDT):
> class Module
> def attr_bool sym
> attr_accessor sym
> alias_method "#{sym}?", sym
> remove_method sym
> end
> end

It's a minor issue, but I think this is more consistent with the
attr_accessor behavior:

class Module
  def bool_accessor *syms
    attr_accessor *syms
    syms.each { |sym| alias_method "#{sym}?", sym }
    remove_method *syms
  end
end

--
William <wmorgan-ruby-talk@masanjin.net>

--
Edwin Eyan Moragas
Main Expectorant

haaktu technologies
spewing spit since '73