[Ruby Package Standard] name correspondence

I

Intransition

The proposed Ruby Package Standard states:

File names and directory structure SHOULD correspond like this:

Library: foo-bar
Directory: lib/foo/bar
Namespace: Foo::Bar

Library: foo_bar
Directory: lib/foo_bar
Namespace: FooBar

That being the case I think Ruby needs to include methods to make it
easy to translate between these. In ActiveSupport these methods are
String#pathize and String#methodize. I would think there would also be
something like String#modulize.

Without such methods I think it is a bit much to ask of developers to
keep this correspondence. I have code that needs to do such
translations dynamically and because Ruby does not have such methods,
I used a FooBar -> foobar.rb correspondence instead (easily
transformed via #downcase). A fact that has led me to generally use
this correspondence throughout all my projects.

I'm all for the proposed standard, but Ruby needs to support it too.
 
B

Brian Candler

Thomas said:
Library: foo-bar
Directory: lib/foo/bar
Namespace: Foo::Bar

Library: foo_bar
Directory: lib/foo_bar
Namespace: FooBar

That being the case I think Ruby needs to include methods to make it
easy to translate between these. In ActiveSupport these methods are
String#pathize and String#methodize. I would think there would also be
something like String#modulize.

Perhaps you're thinking of camelize and underscore in
lib/active_support/inflector.rb

# Examples:
# "active_record".camelize # => "ActiveRecord"
# "active_record".camelize:)lower) # => "activeRecord"
# "active_record/errors".camelize # =>
"ActiveRecord::Errors"
# "active_record/errors".camelize:)lower) # =>
"activeRecord::Errors"

# Examples:
# "ActiveRecord".underscore # => "active_record"
# "ActiveRecord::Errors".underscore # => active_record/errors

Each of these methods is only a few lines. I've seen them copy-pasted in
several projects...
 
R

Rick DeNatale

Perhaps you're thinking of camelize and underscore in
lib/active_support/inflector.rb

=A0 =A0# Examples:
=A0 =A0# =A0 "active_record".camelize =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# = =3D> "ActiveRecord"
=A0 =A0# =A0 "active_record".camelize:)lower) =A0 =A0 =A0 =A0# =3D> "acti= veRecord"
=A0 =A0# =A0 "active_record/errors".camelize =A0 =A0 =A0 =A0 # =3D>
"ActiveRecord::Errors"
=A0 =A0# =A0 "active_record/errors".camelize:)lower) # =3D>
"activeRecord::Errors"

=A0 =A0# Examples:
=A0 =A0# =A0 "ActiveRecord".underscore =A0 =A0 =A0 =A0 # =3D> "active_rec= ord"
=A0 =A0# =A0 "ActiveRecord::Errors".underscore # =3D> active_record/error= s

Each of these methods is only a few lines. I've seen them copy-pasted in
several projects...

There's also constantize

ruby-1.8.7-p173 > "ActiveRecord".constantize
=3D> ActiveRecord
ruby-1.8.7-p173 > "active_record".camelize.constantize
=3D> ActiveRecord
ruby-1.8.7-p173 > "ActiveRecord::Errors".constantize
=3D> ActiveRecord::Errors

Also a few lines of code, although there are two versions one for Ruby
1.9.2 because of changes to Module#const_get


--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
I

Intransition

Perhaps you're thinking of camelize and underscore in
lib/active_support/inflector.rb

Oh? My bad, I thought it had #pathize and #methodize too.
=A0 =A0 # Examples:
=A0 =A0 # =A0 "active_record".camelize =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# = =3D> "ActiveRecord"
=A0 =A0 # =A0 "active_record".camelize:)lower) =A0 =A0 =A0 =A0# =3D> "act= iveRecord"
=A0 =A0 # =A0 "active_record/errors".camelize =A0 =A0 =A0 =A0 # =3D>
"ActiveRecord::Errors"
=A0 =A0 # =A0 "active_record/errors".camelize:)lower) # =3D>
"activeRecord::Errors"

=A0 =A0 # Examples:
=A0 =A0 # =A0 "ActiveRecord".underscore =A0 =A0 =A0 =A0 # =3D> "active_re= cord"
=A0 =A0 # =A0 "ActiveRecord::Errors".underscore # =3D> active_record/erro= rs

Each of these methods is only a few lines. I've seen them copy-pasted in
several projects...

In Facets those are #camelcase and #snakecase, but it also has the
other methods -- No problem getting access to such methods. Just think
Ruby needs to support something along these lines out the box if the
correspondence should be considered a standard.
 

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
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top