Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Sets in Ruby

Tags:

ruby

set

I'm building a simple Ruby on Rails plugin and I'm considering using the Set class. I don't see the Set class used all too often in other people's code.

Is there a reason why people choose to use (subclasses of) Array rather than a set? Would using a set introduce dependecy problems for some people?

like image 685
avdgaag Avatar asked Dec 30 '22 17:12

avdgaag


2 Answers

Set is part of the standard library, so it shouldn't pose any dependency problems. If it's the cleanest way to solve the problem, go for it.

Regarding use (or lack thereof) I think there are probably two main reasons:

  • programmers not being aware of the library
  • programmers not realising when sets are the best way to a solution
  • programmers not knowing/remembering anythign about sets at all.

Make that three main reasons.

like image 161
Mike Woodhouse Avatar answered Jan 03 '23 09:01

Mike Woodhouse


Ruby arrays are very flexible anyway, there are plenty of methods that allow to threat It like a set, a stack or a queue for example.

like image 32
José Joel. Avatar answered Jan 03 '23 09:01

José Joel.