tags:

views:

114

answers:

2

What is the idiomatic way to add an include guard in a ruby file analogous to

#ifdef FOO_H
#define FOO_H
...

#endif

in C?

+10  A: 

If you use require to load your file, it will only be included once (assuming you're not loading the same file by different names/paths), so in general Ruby files do not use include guards.

Chris Jester-Young
+3  A: 

As Chris Jester-Young says, require should be all you need. That said, you can use a simple if or similar as an "include" guard if you want to make yourself one, e.g.:

unless defined? FooGuard
  FooGuard = true
  ... rest of code ...
end

You can do anything in the body of the if, including define classes, methods, etc.

Logan Capaldo