Ruby で alias を使う
Ruby
Published: 2019-06-17

やったこと

Ruby で alias を使ってみます。

確認環境

$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]

調査

app/controllers/tasks_controller.rb

class TasksController < ApplicationController
  def new
    logger.debug(__method__)
  end

  def new222
    logger.debug(__method__)
  end

  alias :new :new222
end

ログの確認

Started GET "/tasks/new" for ::1 at 2019-06-15 18:18:07 +0900
   (0.3ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
  ↳ /Users/xxxx/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
path is /tasks/new
Processing by TasksController#new as HTML
new222
  Rendering tasks/new.html.erb within layouts/tasks
  Rendered tasks/new.html.erb within layouts/tasks (264.0ms)
Completed 200 OK in 290ms (Views: 282.6ms | ActiveRecord: 0.0ms)

new222 が呼ばれます。

参考