avdgaag
avdgaag

Reputation: 42142

Using Sets in Ruby

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?

Upvotes: 4

Views: 1170

Answers (3)

José Joel.
José Joel.

Reputation: 2050

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.

Upvotes: 2

Mike Woodhouse
Mike Woodhouse

Reputation: 52326

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.

Upvotes: 8

Matthew Scharley
Matthew Scharley

Reputation: 132284

The way I've understood it from what I've read here is that Set's are likely mathematical sets, ie. order doesn't matter and isn't preserved.

In most programming applications, unless you're doing maths, it have limited use, because normally you want to preserve order.

Upvotes: 0

Related Questions