Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR RUBY

ruby create class method

# class << self opens up self's singleton class, so that methods can be redefined for the current self object
class String
  class << self
    def value_of obj
      obj.to_s
    end
  end
end

String.value_of 42   # => "42"

# This can also be written as a shorthand:

class String
  def self.value_of obj
    obj.to_s
  end
end

# Even shorter
def String.value_of obj
  obj.to_s
end
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #ruby #create #class #method
ADD COMMENT
Topic
Name
1+3 =