Hacker News new | ask | show | jobs
by CodeIsTheEnd 1530 days ago
TIL: #method(:method_name) in Ruby

I'm a big fan of Ruby, and feel like I know the language pretty well, but, my goodness, I don't know how I've never come across this one before. THIS is something that feels like magic!!

    [1]> 3.method(:days).source_location
    => [".../lib/active_support/core_ext/numeric/time.rb", 37]

    [2]> puts 3.method(:days).comment
    # Returns a Duration instance matching the number of days provided.
    #
    #   2.days # => 2 days
    => nil

    [3]> puts 3.method(:days).source
      def days
        ActiveSupport::Duration.days(self)
      end
    => nil
https://ruby-doc.org/core-3.1.1/Method.html