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
 end
end


在 清单 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 方法编写属性声明代码,指出属性是既可读又 可写的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值