What's the Difference Between These Ruby Namespace Conventions

What's the difference between these Ruby namespace conventions?

The difference lies in nesting.

In the example below, you can see that the former method using class Foo, can get the outer scope's constant variables BAR_A without errors.

Meanwhile, class Baz will bomb with an error of uninitialized constant A::B::Baz::BAR_A. As it doesn't bring in A::* implicitly, only A::B::*explicitly.

module A
BAR_A = 'Bar A!'
module B
BAR_B = 'Bar B!'
class Foo
p BAR_A
p BAR_B
end
end
end

class A::B::Baz
p BAR_A
p BAR_B
end

Both behaviors have their place. There's no real consensus in the community in my opinion as to which is the One True Ruby Way (tm). I personally use the former, most of the time.

Ruby namespace etiquette and convention

I would stay out of their namespace (whatever you end up choosing to name your extension namespace) because that means you aren't going to accidentally collide with something they decide to do down the road.

Difference between . and #

The hash format (Class#method) is not valid ruby, but is used in documentation to describe an instance method.

Class methods are typically documented using a double-colon (Class::method).

You will see examples of both in the ruby docs (e.g. http://www.ruby-doc.org/core-1.9.3/String.html)

The dot format is used in code when actually calling a class method (Class.method), though I have seen some people (unfortunately) use it interchangeably with either the double-colon or hash in documentation.

what is the meaning of ? and ! in Ruby

Ruby's syntax allows you to postfix a single ? or ! to your method names (both or more than one of each is invalid).

This is to support a couple of deeply ingrained conventions:

method?:

A method that returns a boolean and doesn't modify its receiver's state should use a ?. For example, if you want to "ask" whether a container is sorted, you might use my_obj.sorted?. If you want to "ask" whether a container is empty, you might use my_obj.empty?. Neither of these methods would modify their receiving object's state. See String for many examples of ? methods being used to interrogate an object without modifying it, such as start_with? and end_with?.

method!:

A method that has a ! at the end destructively modifies its objects state. By extension, when a method might destructively transform an object's state, there are often two versions of the method provided, with and without a !. In the case of an array, you might have a pair of methods called sort and sort!. sort will return a copy of the array, in sorted order, without modifying the original copy. After calling sort, you will have two copies, including an unmodified version of the original. sort! however, will sort the array in-place, overwriting the original array.

Again, referring to String, we have methods such as capitalize which returns a capitalized copy of a string, and capitalize! which capitalizes the string in-place:

x = "what"
y = x.capitalize
puts x # "what"; x is unchanged
puts y # "What"

x = "what"
y = x.capitalize!
puts x # "What"; x is changed in-place
puts y # "What"

It's worth noting that Rails has its own conventions for the semantics of !. In Rails, a method that can fail (such as a database write) will typically return true or false. Rails uses a ! to indicate that the method should fail with an exception, instead of a false return value. For example, my_record.save will return false on failure; my_record.save! will raise an exception.

Standard File Naming Conventions in Ruby

With just Ruby (i.e. not Rails), naming is only a convention. In Rails the convention of using underscores is necessary (almost).

I think convention #2 lowercase_and_underscore.rb is more common and looks pretty good, though an article Here says lowercasenounderscore.rb is the Ruby convention.

Pick either which ever convention is more common or which ever one you like more. The most important thing is to be consistent within a project.



Related Topics



Leave a reply



Submit