new ("alice")}? I am new to Ruby and I am using the following code today. I searched for the book "The R...">

In Ruby, what does "new" mean in {1 => new ("alice")}?

I am new to Ruby and I am using the following code today. I searched for the book "The Ruby Programming Language" but did not find an explanation of this syntax. Can anyone help explain? I know you need to use something like Person.new("My name")

.

class Person
  attr_reader :name
  def initialize name
    @name = name
  end

  def self.find id
    people = {1 => new("alice"), 2 => new("bob")}
    people[id]
  end
end

      

+3


source to share


1 answer


find

- class method.

In a class method, self

refers to a class. It self

can be omitted in the method .



So it new

means self.new

; which is equivalent in this case Person.new

.

+6


source







All Articles