How to design a class which has a list of boolean variables?

I have a Ruby class called Category which has various boolean values like

  • sport
  • food
  • education
  • entertainment

So I will associate this Category class with some other class to define its category, basically the issue is all variables within this class are just booleans. Is it best to use 4 different variables in Ruby class or use an integer and encode the booleans into it?

But if I encode the code becomes not so flexible incase I want to add another variable to this case, so what's the best way to do this?

Answers


Wouldn't it be better if you represent the categories with an array of symbols like categories = [:sport,:food] ? Probably you're checking something like if (sport) in your code, which could be replaced by if (categories.include? :sport) (which in my opinion is even more meaningful). This way, you would have an array with the "true values" instead of having lots of boolean variables, which is way more extensible and maintainable and seems to achieve what you need.

Anyway, I'm lacking more information about your classes, maybe if you can provide more information about your classes I could give you better advice.


Need Your Help

Laravel get model attribute: difference between two methods

php laravel model attributes eloquent

I found out that there are two ways to get/display a model's attribute with Laravel. I could create a function in User.php for example:

Failure with running TorqueBox on Ubuntu Quantal

ruby jruby torquebox

I'm trying to set up TorqueBox inside Vagrant on Ubuntu Quantal. I've deployed my app into TorqueBox, but when I try to run bin/standalone.sh, it hangs for a long time after "Setting up Bundler" an...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.