About Ruby File Class Extensions

Asked 2 years ago, Updated 2 years ago, 34 views

I want to extend ruby's file class, but it doesn't work.

The ruby version is 2.0.0p648.
http://qiita.com/umanoda/items/33613b94ccabf7b1f851
I tried using this article as a reference.
I want 'extend' to be displayed, but the open method returns normal operation

Could someone please teach me?

class File
  alias_method:__open__, —open

  default()
    'extend'
  end
end

pFile.open('test.txt')
#<File:test.txt>

Thank you for your cooperation.

ruby

2022-09-29 22:38

1 Answers

Because you overwrite the class method, you will have to define the class method instead of the instance method.

class File
  class<<self
    alias_method:__open__, —open
  end

  def self.open(*args)
    'extend'
  end
end

pFile.open('test.txt')


2022-09-29 22:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.