Scope Resolution Operator - Ruby

Ruby

In Ruby, scope resolution can be specified using namespaces (such as classes or modules).

module Example Version = 1.0 class << self # We are accessing the Module's singleton class def hello(who = "World") "hello #{who}" end end end #/Example Example::hello # => "hello World" Example.hello "hacker" # => "hello hacker" Example::Version # => 1.0 Example.Version # NoMethodError # This illustrates the difference between the message (.) operator and the scope operator in Ruby (::) # We can use both ::hello and .hello, because hello is a part of Example's scope and because Example # responds to the message hello. # # We can't do the same with ::Version and .Version, because Version is within the scope of Example, but # Example can't respond to the message Version, since there is no method to respond with.

Read more about this topic:  Scope Resolution Operator

Famous quotes containing the word ruby:

    Lay down, lay down the bigly bier,
    Lat me the dead look on;
    Wi’ cherry cheeks and ruby lips
    She lay an’ smil’d on him.

    O ae sheave o’ your bread, true-love,
    An’ ae glass o’ your wine,
    For I hae fasted for your sake
    These fully day [is] nine.
    Anna Gordon Brown (1747–1810)

    Thy great leaves enfold
    The ancient beards, the helms of ruby and gold
    Of the crowned Magi; and the king whose eyes
    Saw the Pierced Hands and Rood of elder rise
    In Druid vapour and make the torches dim....
    William Butler Yeats (1865–1939)

    A man in the house is worth two in the street.
    Mae West, U.S. actor, screenwriter, and Leo McCarey. Ruby Carter (Mae West)