Skip to main content

Diigo Home

Ruby Programming/Classes and objects - Wikibooks, collection of open-content t... - The Diigo Meta page

en.wikibooks.org/...Classes_and_objects - Cached - Annotated View

Benx Shen's personal annotations on this page

benxshen
Benxshen bookmarked on 2009-10-24
  •   class Strawberry
    def Strawberry.color
    return "red"
    end

         def self.size
    return "kinda small"
    end

         class << self
    def shape
    return "strawberry-ish"
    end
    end
    end
    Strawberry.color # -> "red"
    Strawberry.size # -> "kinda small"
    Strawberry.shape # -> "strawberry-ish"

    Note the three different constructions: ClassName.method_name and self.method_name are essentially the same - outside of a method definition in a class block, self refers to the class itself. The latter is preferred, as it makes changing the name of the class much easier. The last construction, class << self, puts us in the context of the class's "meta-class" (sometimes called the "eigenclass")

This link has been bookmarked by 1 people . It was first bookmarked on 24 Oct 2009, by Benx Shen.

  • 24 Oct 09
    •   class Strawberry
      def Strawberry.color
      return "red"
      end

           def self.size
      return "kinda small"
      end

           class << self
      def shape
      return "strawberry-ish"
      end
      end
      end
      Strawberry.color # -> "red"
      Strawberry.size # -> "kinda small"
      Strawberry.shape # -> "strawberry-ish"

      Note the three different constructions: ClassName.method_name and self.method_name are essentially the same - outside of a method definition in a class block, self refers to the class itself. The latter is preferred, as it makes changing the name of the class much easier. The last construction, class << self, puts us in the context of the class's "meta-class" (sometimes called the "eigenclass")