新手问题 类属性的默认值的问题

jofone · February 23, 2013 · Last by jofone replied at February 24, 2013 · 2682 hits

如何在Ruby类中为某些属性设置默认值?比如C#的类:

class ClassA
{
    public string A {get; set; }
    public string B {get; set; }

    public ClassA()
    {
        this.A = "aaa";
    }
}

如何在 Ruby 中实现这样的效果?

你写在 initializer 里就行了

谢谢 iBachue 和 zlx star,我找到解决的办法了,直接在 Rails 的迁移文件中,为属性添加:default 就可以了。

You need to Sign in before reply, if you don't have an account, please Sign up first.