Skip to content

Instantly share code, notes, and snippets.

View dmuneras's full-sized avatar

Daniel Múnera Sánchez dmuneras

View GitHub Profile
@damien-roche
damien-roche / rubymethodlookup.md
Last active January 16, 2024 10:40
A Primer on Ruby Method Lookup

A Primer on Ruby Method Lookup

Method lookup is a simple affair in most languages without multiple inheritance. You start from the receiver and move up the ancestors chain until you locate the method. Because Ruby allows you to mix in modules and extend singleton classes at runtime, this is an entirely different affair.

I will not build contrived code to exemplify the more complicated aspects of Ruby method lookup, as this will only serve to confuse the matter.

When you pass a message to an object, here is how Ruby finds what method to call:

1. Look within singleton class

@mudge
mudge / gist:786953
Last active November 3, 2021 08:59
How to pass blocks between methods in Ruby < 3.0.0 without using &block arguments.
# UPDATE: The ability to call Proc.new without a block was removed in Ruby 3.0.0
# See https://bugs.ruby-lang.org/issues/10499 for more information.
# So you might have heard that this is slow:
#
# def some_method(&block)
# block.call
# end
#
# Compared to:
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}