The REST API railscast has the following code example:
module Api
module V1
class ProductsController < ApplicationController
class Product < ::Product
def as_json(options={})
super.merge(released_on: released_at.to_date)
end
end
end
end
end
I’m having trouble following what:
class Product < ::Product
…does? When I try to recreate something similar in irb I get:
module Fooirb(main):001:0> module Foobar
irb(main):002:1> class Product < ::Product
irb(main):003:2> end
irb(main):004:1> end
NameError: uninitialized constant Product
from (irb):2:in `<module:Foobar>'
from (irb):1
The
::token indicates a namespace, and if used without anything directly before, it accesses the top-level namespace. So for example, take this small program:If you call
Foo.new_bar(:top), the messageNew bar built in Rubytown, USAgets printed. If instead you use, say,Foo.new_bar(:place_to_drink)it instead printsNew ingot created“.