::Base part meaning in ActiveRecord::Base

What does ::Base part mean in Person < ActiveRecord::Base class declaration? I'm new to ruby and from what I've gathered so far, Person < ActiveRecord should be used. Thank you.

Answers


::Base is a class in module ActiveRecord. One of the things modules do is provide namespacing in Ruby. In Ruby you don't inherit from a module but you can mix it in using the include statement.

May I suggest picking up the Pickaxe book or reading Why's (Poignant) Guide to Ruby.


in Ruby, :: accesses static class or module constants. ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

Edit: as Mike points out, in this case ActiveRecord is a module...


:: is a unary operator that lets you access a constant, module, or class defined inside another class or module. It is used to provide namespaces so that method and class names don't conflict with other classes by different authors.

When you see ActiveRecord::Base in Rails it means that in Rails ActiveRecord::Base is something like this

module ActiveRecord
  class Base
  end
end

This means that a class called Base is inside a module ActiveRecord which is then referenced as ActiveRecord::Base

For better understanding of :: operator, just go through this example from tutorialspoint.com :

MR_COUNT = 0        # constant defined on main Object class
module Foo
  MR_COUNT = 0
  ::MR_COUNT = 1    # set global count to 1
  MR_COUNT = 2      # set local count to 2
end
puts MR_COUNT       # this is the global constant
puts Foo::MR_COUNT  # this is the local "Foo" constant

Need Your Help

Null checks on extension methods that do not need an instance?

c# null extension-methods

I recently asked this question on the subject, which was closed as a duplicate of this one - as was this one before mine. However, I want to ask a more specific question on a corner case that I fee...

Shifting a char array prior to adding to it

c arrays insert char shift

I want to insert characters into the middle of a char array in C, but first I want to shift it to the right each time prior to adding a char so that I don't lose what's already in the char array (c...