1

In my Ruby script, I'm declaring a constant outside of a method:

SYNDICATIONS = %w(Advanced Syndication, Boxee feed, Player MRSS, iPad MRSS, iPhone MRSS, YouTube)

and iterating it in a method like:

def some_method
   SYNDICATIONS.each do |syndication|
      puts syndication
   end
end

Is iterating over a constant a good idea or not?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Alpha
  • 13,320
  • 27
  • 96
  • 163
  • 3
    I don't see anything bad. You should check the commas in your `%w` literal, though. I think that you don't get the result you expect. – Sergio Tulentsev May 10 '13 at 06:46
  • there is nothing wrong in the code. I just want to make sure that this is not a bad way of writing code. – Alpha May 10 '13 at 06:50
  • 2
    "there is nothing wrong in the code." As @SergioTulentsev kindly pointed out, you have commas inside a `%w[...]` definition, which will result in words/elements with trailing commas. If nothing else that creates a maintenance problem because it's a very odd and unexpected combination, which would confuse someone debugging the code. In a code review I'd request that it be documented with a comment, or changed to a normal array definition, to avoid confusion. – the Tin Man May 10 '13 at 07:44

3 Answers3

5

There's nothing wrong with iteration. There is an error with the definition of your constant, though. %w operator doesn't work as you probably think it does. It splits tokens on a whitespace, not comma. If you want the space to not be delimiter, you escape it. Compare these three examples and see which is the clearest.

a1 = %w(Advanced Syndication, Boxee feed, Player MRSS, iPad MRSS, iPhone MRSS, YouTube)
a1 # => ["Advanced", "Syndication,", "Boxee", "feed,", "Player", "MRSS,", "iPad", "MRSS,", "iPhone", "MRSS,", "YouTube"]

a2 = %w(Advanced\ Syndication Boxee\ feed Player\ MRSS iPad\ MRSS iPhone\ MRSS YouTube)
a2 # => ["Advanced Syndication", "Boxee feed", "Player MRSS", "iPad MRSS", "iPhone MRSS", "YouTube"]

a3 = ["Advanced Syndication", "Boxee feed", "Player MRSS", "iPad MRSS", "iPhone MRSS", "YouTube"]
a3 # => ["Advanced Syndication", "Boxee feed", "Player MRSS", "iPad MRSS", "iPhone MRSS", "YouTube"]
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • Thanks for the correction. My above code wasn't generating what I wanted. You corrected it thank you! – Alpha May 10 '13 at 07:00
4

You aren't iterating over the constant — you are iterating over an Array, which happens to be referenced by a constant. The constant itself is just a kind of variable that isn't meant to be reassigned. But you don't deal with the constant itself — you deal with the object it references.

So the question becomes: Is it OK to iterate over an array?

And the obvious answer to that question is: Yes.

Chuck
  • 234,037
  • 30
  • 302
  • 389
0

Besides the error in %w pointed out by Sergio, the code would work, but if the particular code you have is what you are trying to do with it, then it is not a good way. This would be better:

def some_method
  puts SYNDICATIONS
end
sawa
  • 165,429
  • 45
  • 277
  • 381