4-3 オブジェクト

In [1]:
class MyClass
end

MyClass.superclass
Out[1]:
Object

4-3-1 オブジェクトの基本的な振る舞い

In [2]:
o = Object.new

p o.class
p o.is_a?(Object)
p o.object_id
p o.nil?
p o.frozen?
Object
true
70194085268280
false
false
Out[2]:
false
In [8]:
s = "hoge".freeze
s
Out[8]:
"hoge"
In [9]:
s.upcase!
RuntimeError: can't modify frozen String
(pry):20:in `upcase!'
(pry):20: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 [10]:
s = 'foo'
Out[10]:
"foo"
In [12]:
CONST_1 = 'bar'.freeze
Out[12]:
"bar"
In [13]:
CONST_1.frozen?
Out[13]:
true
In [14]:
CONST_1 = "BAAAAAR"
(pry):25: warning: already initialized constant CONST_1
(pry):23: warning: previous definition of CONST_1 was here
Out[14]:
"BAAAAAR"
In [15]:
p CONST_1
"BAAAAAR"
Out[15]:
"BAAAAAR"
In [16]:
require 'delegate'
Out[16]:
false
In [17]:
Delegator.superclass
Out[17]:
BasicObject
In [18]:
o.tap {|v| puts v }
#<Object:0x007faeab091670>
Out[18]:
#<Object:0x007faeab091670>
In [ ]:
Recipe.make.tap do |r|
  Author.make(recipd_id: r.id)
end
In [24]:
p(Object.new == Object.new)
p([1,2,3] == [1,2,3])
/pattern/ == /pattern/
false
true
Out[24]:
true
In [25]:
class Ruler
  attr_accessor :length
  
  def initialize(length)
    self.length = length
  end
end

r1 = Ruler.new(10)
r2 = Ruler.new(10)
r1 == r2
Out[25]:
false
In [26]:
class Ruler
  attr_accessor :length
  
  def initialize(length)
    self.length = length
  end
  
  def ==(other)
    length == other.length
  end
end

r1 == r2
Out[26]:
true
In [27]:
r1 != r2
Out[27]:
false
In [28]:
r2.length = 40
p(r1 == r2)
r1 != r2
false
Out[28]:
true