Hello all,

       What is significance of :: in ActiveRecord::Base.

What is difference between ::String.new and String.new


Thank you,
Praveen

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/304bcd8d9bd91d1a9f7750504d96ee17%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Mateusz Dw at Jul 15, 2014 at 7:06 am
    Good question!

    Using :: you have access to the class inside the module. Module can nest
    another modules and so on. :: its a namespace resolution operator.

    Call ::String.new inside some module back you to top-level namespace
    (outside the module)

    Here is examples

    class String
      def initialize
        puts "im string outside module"
      end
    end

    module ActiveRecord
       module AnotherModule
       end

       class String
        def initialize
          puts "im string inside module"
        end
       end

       class Base
        def initialize
         ::String.new
         String.new
        end
       end
    end

    Here are results from IRB:

    1.9.3-p392 :021 > ActiveRecord::Base.new
    im string outside module
    im string inside module
      => #<ActiveRecord::Base:0xa024fb8>
    1.9.3-p392 :022 > ActiveRecord::String.new
    im string inside module
      => #<ActiveRecord::String:0xa023168>

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/16b571bc5841c8303874e67adafec595%40ruby-forum.com.
    For more options, visit https://groups.google.com/d/optout.
  • Eugene Gilburg at Jul 15, 2014 at 8:11 am
    ::String means explicit root namespace.

    So if you have something like
    module MyNamespace
       class String
          ...
       end
    end

    MyNamespace::String will be your own class, while ::String will still be
    the original string class defined by Ruby. If you just type String, it'll
    find the closest matching namespace, so if you call String.new from within
    MyNamespace, it'll return MyNamespace::String, while if you call String.new
    from outside MyNamespace, it'll call the default Ruby string. Writing
    ::String means you explicitly want the default Ruby string, even if you
    call it from inside MyNamespace.
    On Monday, July 14, 2014 11:12:01 PM UTC-7, Ruby-Forum.com User wrote:

    Hello all,

    What is significance of :: in ActiveRecord::Base.

    What is difference between ::String.new and String.new


    Thank you,
    Praveen

    --
    Posted via http://www.ruby-forum.com/.
    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bf38b8ff-7018-4530-b36c-ad493fc4daed%40googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJul 15, '14 at 6:11a
activeJul 15, '14 at 8:11a
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Mateusz Dw: 2 posts Eugene Gilburg: 1 post

People

Translate

site design / logo © 2022 Grokbase