How can one set property values when initializing an object in Ruby?

前端 未结 8 2123
清歌不尽
清歌不尽 2021-01-01 16:09

Given the following class:

class Test
  attr_accessor :name
end

When I create the object, I want to do the following:

t = Test         


        
8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 16:16

    The code you're indicating is passing parameters into the initialize function. You will most definitely have to either use initialize, or use a more boring syntax:

    test = Test.new
    test.name = 'Some test object'
    

提交回复
热议问题