tags:

views:

129

answers:

3
+1  Q: 

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?

A: 

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.

Matthew Scharley
I'm looking at a collection of tags for an object. Tags are unique, order is irrelevant. Sounds like a good fit, no?
avdgaag
Indeed, it is a good fit. Ignore the bad advice given in the last paragraph.
Pinochle
I didn't say useless, just that alot of programming applications do need order. Don't put words in my mouth.
Matthew Scharley
No one implied that you said sets were useless. Your advice, however, is without context and thus it does not help. You claim that most computational tasks require maintaining order in a collection, and that would need to be supported. Personally, I use sets all the time and I don't write numerical applications as a rule. Perhaps you solve problems in such a way that you don't use sets, but that would be a question of personal style in problem solving that can't be generalized.
Pinochle
Sets are very often useful.
Alexey Romanov
Indeed. I've seen many programs that use hashes just for the set-like behaviour of hash keys.
glenn jackman
+6  A: 

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.

Mike Woodhouse
So you reckon Set would always be available...? I have no idea how ‘standard’ the standard library is, and to what extent you really can depend on it always being available on hosts.
avdgaag
It's part of Ruby's standard library. You can get the basic docs by hitting `ri Set` on the machine where you intend to use it. In essence, if you have access to Ruby, you have access to the `Set` library
Pinochle
+2  A: 

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.

José Joel.
I +1'd because this is likely why ruby programmer's don't use the set class. Ruby's arrays are usually plenty capable.
Allyn
But wouldn't it make more sense to use the Set class if, as in this case, you require no duplicates? Why re-invent the wheel and subclass array, add a bunch of logic to prevent duplicates? This is not a rhetorical question -- I want to know. That's the point of the question.
avdgaag