属性编写器显示密码

时间:2018-03-13 14:48:19

标签: ruby-on-rails

我刚开始在铁轨上学习红宝石并使用 http://ruby-for-beginners.rubymonstas.org/writing_classes/attribute_writers.html

文章没有解释如何显示密码

这是我的代码。

class Person
  def initialize(name)
    @name = name
  end

  def name
    @name
  end

  def password=(pass)
    @password = pass
  end

  def greet(other)
    puts "Hi " + other.name
    puts "Your password is " + #(how do i call password here?)
  end

end

  person = Person.new("Lee")
  person.password = ("super secret")
  person.greet(person)
  p person

我此时并不理解属性作者。 任何人都可以帮助我吗? 非常感谢你

1 个答案:

答案 0 :(得分:0)

虽然您已经设置了类变量@password = pass

您可以将其称为@password,请记住,如果密码未设置,则会出错。

  def greet(other)
    puts "Hi " + other.name
    puts "Your password is #{@password}"
  end

person = Person.new("Lee")
person.greet(person) #will give an error
person.password = "pass"
person.greet(person) #will print the password