如何将变量的值用作类变量访问器

时间:2018-11-21 21:05:03

标签: ruby

例如:

class Student 
  attr_accessor :name
  def initialize(name)
      @name=name
  end
end

s1 = Student.new("student1")
var = "name"
puts s1.var  # error message, var is not defined in class Student

我希望puts s1.varputs s1.name一样打印“ student1”

0 个答案:

没有答案