Constructor

Download ruby eBook

Example

A class can have only one constructor, that is a method called initialize. The method is automatically invoked when a new instance of the class is created.

class Customer
  def initialize(name)
     @name = name.capitalize 
  end
end

sarah = Customer.new('sarah')
sarah.name #=> 'Sarah'

Stats

Contributors: 4
2016-09-01
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook