4-1-3 インスタンス変数へのアクセス

In [3]:
class Ruler
  attr_accessor :length
end

class Ruler2
  def length=(length)
    @length = length
  end
  
  def length
    @length
  end
end
  
ruler = Ruler.new
p ruler.length
p ruler.length = 30
p ruler.length
nil
30
30
Out[3]:
30
In [4]:
ruler2 = Ruler2.new
p ruler2.length
p ruler2.length = 30
p ruler2.length
nil
30
30
Out[4]:
30
In [5]:
class Hoge
  attr_accessor :foo?
end
NameError: invalid attribute name `foo?'
(pry):56:in `attr_accessor'
(pry):56:in `<class:Hoge>'
(pry):55:in `<main>'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:323:in `handle_line'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `block in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:65:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:12:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:87:in `execute_request'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:47:in `dispatch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:37:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:70:in `run_kernel'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:34:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/bin/iruby:5:in `<top (required)>'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `load'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `<main>'
In [6]:
class Rectangle
  attr_accessor :width, :heght
end

# 上と同じ
class Rectangle2
  attr_reader  :width, :heght
  attr_writer  :width, :heght
end

4-1-4 クラスメソッド

In [8]:
class Ruler
  attr_accessor :length
  
  def self.pair
    [self.new, self.new]
  end
end

Ruler.pair
Out[8]:
[#<Ruler:0x007fdf5c938c88>, #<Ruler:0x007fdf5c938c38>]
In [10]:
class Ruler
  attr_accessor :length
  
  def double_length
    length * 2
  end
  private :double_length
  
  class << self
    def pair
      [new, new]
    end
    
    def trie
      [new, new, new]
    end
  end
end

Ruler.pair
Out[10]:
[#<Ruler:0x007fdf5d819108>, #<Ruler:0x007fdf5d8190b8>]
In [11]:
ruler = Ruler.new
ruler.double_length
NoMethodError: private method `double_length' called for #<Ruler:0x007fdf5d8fd9c0>
(pry):127:in `<main>'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:323:in `handle_line'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:242:in `block in eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `catch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/pry_instance.rb:241:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:65:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/backend.rb:12:in `eval'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:87:in `execute_request'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:47:in `dispatch'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/kernel.rb:37:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:70:in `run_kernel'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/lib/iruby/command.rb:34:in `run'
/Users/mic/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/iruby-0.2.7/bin/iruby:5:in `<top (required)>'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `load'
/Users/mic/.rbenv/versions/2.2.2/bin/iruby:23:in `<main>'