Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Boolean attribute naming convention and use

Learning ruby. I'm under the impression that boolean attributes should be named as follows:

my_boolean_attribute? 

However, I get syntax errors when attempting to do the following:

class MyClass   attr_accessor :my_boolean_attribute?    def initialize     :my_boolean_attribute? = false   end end 

Apparently ruby is hating the "?". Is this the convention? What am I doing wrong?

like image 848
Dane O'Connor Avatar asked Aug 11 '09 21:08

Dane O'Connor


People also ask

What's the proper convention for naming variables in Ruby?

Variable names in Ruby can be created from alphanumeric characters and the underscore _ character. A variable cannot begin with a number. This makes it easier for the interpreter to distinguish a literal number from a variable. Variable names cannot begin with a capital letter.

How do you define a boolean in Ruby?

In Ruby, a boolean refers to a value of either true or false , both of which are defined as their very own data types. Every appearance, or instance, of true in a Ruby program is an instance of TrueClass , while every appearance of false is an instance of FalseClass .

What are the boolean values in Ruby?

true and false are Ruby's native boolean values. A boolean value is a value that can only be one of two possible values: true or not true. The object true represents truth, while false represents the opposite.


1 Answers

Edit: three-years later; the times, they are a-changin'…

Julik's answer is the simplest and best way to tackle the problem these days:

class Foo   attr_accessor :dead   alias_method :dead?, :dead # will pick up the reader method end 

My answer to the original question follows, for posterity…


The short version:

You can't use a question mark in the name of an instance variable.

The longer version:

Take, for example, attr_accessor :foo — it's simply conceptually a bit of syntactic sugar for the following:

def foo   @foo end  def foo=(newfoo)   @foo = newfoo end 

Furthermore, the question-mark suffix is mostly just a convention to indicate that the return value of a method is a boolean.

The best approximation I can make of what you're going for here…

class MyClass    def initialize     @awesome = true   end    def awesome?     @awesome   end  end 

In this case, there may be a case to be made for using attr_accessor — after all, it may be explicit that you're working directly with a boolean attribute. Generally, I save the question-mark suffix for when I am implementing a method whose boolean return value is based on slightly more complex conditions than just the value of an attribute.

Cheers!


Edit, two years later, after a recent comment:

  1. Ruby enforces certain naming conventions. Symbols in Ruby can't have question marks. Thus invocations of :my_boolean_attribute? both will fail with a NameError. Edit: not correct, just use the quoted syntax for a symbol, e.g., :"my_attribute?"
  2. Symbols are immutable, attempting to assign to one will throw a SyntaxError.
like image 158
Nick Zadrozny Avatar answered Oct 01 '22 14:10

Nick Zadrozny