4-2-3 メソッドをオブジェクトに取り込む

In [1]:
module Greetable
  def greet_to(name)
    puts "Hello, #{name}. I'm a #{self.class}."
  end
end

o = Object.new
o.greet_to 'hoge'
NoMethodError: undefined method `greet_to' for #<Object:0x007fce5c1c61f0>
(pry):8: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 [2]:
o.extend Greetable
o.greet_to 'hoge'
Hello, hoge. I'm a Object.
In [4]:
class Alice
  extend Greetable
end

Alice.greet_to 'hoge'
Hello, hoge. I'm a Class.
In [5]:
module Bob
  extend Greetable
end

Bob.greet_to 'hoge'
Hello, hoge. I'm a Module.
In [7]:
module Teachable
  def teach
    puts 'You must study!!!'
  end
end

class Human
  extend Teachable
end

Human.teach
You must study!!!

4-2-4 モジュール関数

In [10]:
Math.sqrt(4)
Out[10]:
2.0
In [9]:
include Math
sqrt(4)
Out[9]:
2.0
In [11]:
log(10)
Out[11]:
2.302585092994046
In [12]:
module MyFunction
  def mod_function
    puts 'Called!'
  end
  module_function :mod_function
end

MyFunction.mod_function
Called!
In [16]:
module MyFunction
  def outer_function
    first_function
    second_function
  end
  
  module_function
  
  def first_function
    puts 'first'
  end
  
  def second_function
    puts 'second'
  end
end
Out[16]:
:second_function
In [15]:
MyFunction.second_function
second
In [17]:
o2 = Object.new
o2.extend MyFunction

o2.outer_function
first
second
In [18]:
#  モジュール関数とはprivateなインスタンスメソッドである
o2.first_function
NoMethodError: private method `first_function' called for #<Object:0x007fce5b182be0>
(pry):94: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 [19]:
MyFunction.new
NoMethodError: undefined method `new' for MyFunction:Module
(pry):95: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>'

4-2-5 クラスやモジュールを自動的にロードする

In [20]:
OpenSSL::VERSION
Out[20]:
"1.1.0"
In [22]:
NKF::VERSION
NameError: uninitialized constant NKF
(pry):98: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 [23]:
autoload :NKF, 'nkf'

NKF
Out[23]:
NKF
In [24]:
NKF::VERSION
Out[24]:
"2.1.3 (2013-11-22)"
In [25]:
#import pandas as pd

次回は4-3 オブジェクトから

In [ ]: