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
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='...