convention for internal class/module name?

D

David Garamond

Since Ruby forbid _Foo, what is the convention for internal class/method
name? Currently I use Foo_, but I wonder what other people use.
 
P

Paul Brannan

Since Ruby forbid _Foo, what is the convention for internal class/method
name? Currently I use Foo_, but I wonder what other people use.

What do you mean by "internal"? A class that shouldn't be accessed by
classes other than the one you are writing (that is, it is an
implementation detail)?

If I write a class, I tend to expect to re-use it, so I rarely do this.
But if I do write a class that other code shouldn't have access to, I
have been known to put it in a class variable:

class Foo
@@bar = Class.new
@@bar.class_eval do
def foo
puts "foo!"
end
end

def initialize
@bar = @@bar.new
@bar.foo()
end
end

or in 1.8:

class Foo
@@bar = Class.new do
def foo
puts "foo!"
end
end
end

Paul
 
D

David Garamond

Paul said:
What do you mean by "internal"? A class that shouldn't be accessed by
classes other than the one you are writing (that is, it is an
implementation detail)?

Yes. In my case, it's several different implementation per platform.

module Foo_Win32_
def bar; ...; end
end

module Foo_Linux_
def bar; ...; end
end

module Foo_Bsd_
def bar; ...; end
end

class Foo
case RUBY_PLATFORM
when /win/ then include Foo_Win32_
when /linux/ then include Foo_Linux_
when /bsd/ then include Foo_Bsd_
end

def baz; ...; end
def quux; ...; end
end
 
C

Chris Dutton

Is there some reason you can't more directly:

module FooInternals
case RUBY_PLATFORM
when /win/i
def bar
"Windows"
end
when /bsd/i
def bar
"BSD"
end
when /mac/i
def bar
"Macintosh"
end
end
end

class Foo
include FooInternals
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top