⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 05 - validating and modifying attribute values.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
class Name  # Define default getter methods, but not setter methods.  attr_reader :first, :last  # When someone tries to set a first name, enforce rules about it.  def first=(first)    if first == nil or first.size == 0      raise ArgumentError.new('Everyone must have a first name.')    end    first = first.dup    first[0] = first[0].chr.capitalize    @first = first  end    # When someone tries to set a last name, enforce rules about it.  def last=(last)    if last == nil or last.size == 0      raise ArgumentError.new('Everyone must have a last name.')    end    @last = last  end    def full_name    "#{@first} #{@last}"  end  # Delegate to the setter methods instead of setting the instance  # variables directly.  def initialize(first, last)    self.first = first    self.last = last  endend#---jacob = Name.new('Jacob', 'Berendes')jacob.first = 'Mary Sue'jacob.full_name                                 # => "Mary Sue Berendes"john = Name.new('john', 'von Neumann')john.full_name                                  # => "John von Neumann"john.first = 'john'john.first                                      # => "John"john.first = nil# ArgumentError: Everyone must have a first name.Name.new('Kero, international football star and performance artist', nil)# ArgumentError: Everyone must have a last name.#---class SimpleContainer  attr_accessor :valueendc = SimpleContainer.newc.respond_to? "value="                             # => truec.value = 10; c.value                              # => 10c.value = "some random value"; c.value             # => "some random value"c.value = [nil, nil, nil]; c.value                 # => [nil, nil, nil]#---

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -