Skip to content

Instantly share code, notes, and snippets.

View chrisroos's full-sized avatar

Chris Roos chrisroos

View GitHub Profile

GitHub commit ordering

GitHub's ordering of commits is explained in their ["Why are my commits in the wrong order" help page][github-commit-ordering].

  1. Create new repo on GitHub

  2. Clone locally to /tmp/

    $ cd /tmp
  • gstatic.com/generate_204
  • captive.apple.com

gstatic.com

$ curl -v gstatic.com/generate_204
*   Trying 216.58.213.99...

Ruby's class_eval and line number

Using a heredoc

class Foo
  class_eval(<<-EndCode, __FILE__, __LINE__ + 1)
    def my_method
      raise 'bar'
 end
require 'test/unit'
class Parent
class << self
def my_class_method
'class-method'
end
def bound_class_method
method(:my_class_method)
end

Trying to understand how to categorise a rent deposit payment in FreeAgent.


I'm trying to synchronise the implementations of ClassMethod#restore_original_method and AnyInstanceMethod#restore_original_method in the hope that I can then remove the duplication.

In ClassMethod the @original_method is a Method (i.e. it's bound). In AnyInstanceMethod the @original_method is an UnboundMethod. I presume it's not possible to get hold of a bound method in AnyInstanceMethod as the class hasn't been instantiated at the point we get hold of it.

The define_method method accepts either a Method or UnboundMethod as the second argument. Although I can't use this form in Ruby 1.8.7 because of the "singleton method bound for a different object" problem (see https://gist.github.com/chrisroos/fc62b8e0ce30288abba6a3762b2cb1ee).

Given that I don't think I can get a bound Method in AnyInstanceMethod that means unbinding the method I have in ClassMethod.

I end up with the following code in ClassMethod#restore_original_method:

I've been investigating why the behaviour of ClassMethod#restore_original_method is different between Ruby 1.8.x and all others versions. See [line 77 of ClassMethod][class-method-line-77].

It turns out that there appears to be a problem in 1.8.7 with using define_method when the method argument is a singleton method. This can be illustrated with the following code:

class Foo
  class << self
    def my_class_method
      'Foo.my_class_method'
 end