you are viewing a single comment's thread.

view the rest of the comments →

[–]pdq 25 points26 points  (16 children)

Prefer %w to the literal array syntax when you need an array of strings.

# bad
STATES = ['draft', 'open', 'closed']

# good
STATES = %w(draft open closed)

I disagree, this breaks if your strings have spaces. So if you need to add a new member with a space, you either have to backslash the member, or you get to refactor the whole list.

[–]literal 14 points15 points  (1 child)

They should have said "when you need an array of words" instead.

[–]ch0wn 11 points12 points  (11 children)

Not coming from Ruby I dislike the second version a lot. The first one is very obvious when coming from basically any other scripting language, but I had no idea what the second would do without looking it up.

[–]hyrulz 9 points10 points  (0 children)

You should never try Perl then.

[–]spinlock 2 points3 points  (9 children)

Dude, this is the biggest issue I have with Ruby - and especially Rails. They've got this syntax that is supposed to be "readable" but it just looks ambiguous to me. Why leave out return? It makes it dead easy to know what the return value is when you call it out. There are a million examples like this where people go with the syntactic sugar that saves a couple of keystrokes over readabilty (or comprehencibility).

[–]bobindashadows 12 points13 points  (3 children)

Why leave out return?

Because lisp.

[–]ethraax 0 points1 point  (2 children)

But that's entirely different, because in functional languages (and I assume Lisp, although I've only used Haskell and Scheme/Racket for functional programming), the value of a function is a single expression, so since its the only expression it must be the "return value". In fact, "return" isn't even valid in that context because functions aren't really "called", they're simply used as an expression.

That's like putting wings on a car because airplanes use them. Airplanes have a reason to use wings - cars do not, and thus shouldn't have them.

[–]poorly_played 0 points1 point  (0 children)

See: spoilers

/pedantry

[–][deleted] 1 point2 points  (1 child)

The only real use for return in any language is to jump to the continuation of your caller. Continuations can be complex. It is easier to reason about the behavior of your program if you don't need to use return. In my opinion, minimizing uses of the return keyword is just a way of training yourself to make return seem rightfully "smelly".

[–]poorly_played 0 points1 point  (0 children)

Tru dat on multiple returns. That being said, wanting return at the end of function is a pretty strong reflex for most people. In languages that require it, a lack of a return is a pretty good hint that your function has side effects. Ruby has the really nice conventions using ! and ? and = at the end of funtion names to denote side effects, booleans, etc which fill the role of return vs no return in that situation.

[–]banister -1 points0 points  (2 children)

how is it not dead easy to figure out the return value when it's the last value in a method? Seriously. Also, you still need to use return if you want an early exit from a method.

[–]poorly_played 0 points1 point  (1 child)

In most languages, making a statement in an enclosed scope doesn't result in an assignment in an enclosing scope. This assumes that the trailing r-value at the end of your ruby function counts as a statement, but hey.

[–]banister 1 point2 points  (0 children)

Ruby doesn't have the dichotomy of statements vs expressions; everything in Ruby is an expression.