noodl
noodl

Reputation: 17378

Ruby global variables, legitimate uses

I've never seen global variables used in any Ruby code. I understand that their use is frowned upon across languages but they seem actually useless in Ruby. Can anyone point to properly designed code that uses them?

If I'm right and they're redundant/historical, why do they persist in 1.9?

To be clear, I don't mean variables that Ruby sets up for you like $" and $stdin. I mean uses in one's own code.

Upvotes: 3

Views: 712

Answers (2)

steenslag
steenslag

Reputation: 80065

The only time I see it in decent code is for a log.

$log = Logger.new('foo.log', 'daily')

A constant would probably do fine, but it somehow feels strange calling methods on a constant.

Upvotes: 2

rmk
rmk

Reputation: 4455

Environment variables are usually Global variables in Ruby. So are CLASSPATH in jruby and so on...

Also, you can implement cheap singletons using global variables (although it's not advisable).

So, global variables definitely have a place in Ruby.

Upvotes: 1

Related Questions