rails要理解
清单3
module Dictionary class Word attr_reader :spelling, :part_of_speech, :definitions, :synonyms attr_writer :spelling, :part_of_speech def initialize(spelling, part_of_speech, definitions = [], synonyms = []) @spelling = spelling @part_of_speech = part_of_speech definitions.each{ |idef| idef.word = self} @definitions = definitions @synonyms = synonyms end def add_definition(definition) definition.word = self if definition.word != self @definitions << definition end def add_synonym(synonym) @synonyms << synonym end end class Definition attr_reader :definition, :word, :example_sentences attr_writer :definition, :word def initialize(definition, word = nil, example_sentences = []) @definition = definition @word = word @example_sentences = example_sentences end endend在 清单 3 中的两个类中您可能会注意到,Ruby 支持一种定义属性的简写方式:attr_reader 和 attr_writer。因为使用了这种方式,所以可以在 Word 类中设置 和获取 对应的属性,如清单 7 所示:
清单 7. attr_reader 和 attr_writer 的作用
require "dictionary"wrd = Dictionary::Word.new("turpitude", "Noun")wrd.part_of_speech # "Noun"wrd.spelling # "turpitude"wrd.spelling = "bibulous"wrd.spelling # "bibulous"syns = [Dictionary::Word.new("absorptive", "Adjective"), Dictionary::Word.new("imbibing", "Noun") ]# Danger!wrd.synonyms = syns = syns #Exception: undefined method `synonyms='...attr_reader 和 attr_writer 都不是关键词,而是 Ruby 中的实际方法(在 Module 类中),它们以符号作为参数。符号 是前面有冒号(:)的任何变量,更妙的是符号本身也是对象!
注意,因为在 清单 3 中使 synonyms 成为只读的,所以 Ruby 拒绝执行清单 7 中的最后一行代码。另外,还可以使用 attr_accessor 方法编写属性声明代码,指出属性是既可读又 可写的。