Skip to content

Instantly share code, notes, and snippets.

@brixen
Last active August 29, 2015 14:25
Show Gist options
  • Save brixen/db7705d9874bb58081a7 to your computer and use it in GitHub Desktop.
Save brixen/db7705d9874bb58081a7 to your computer and use it in GitHub Desktop.
How to properly define a common interface across Ruby versions, engines and engine versions
# The goal is to define a common set of methods independent of Ruby version
# engine, and engine version to make other code more simple. The problem is:
# 1. the method may not be defined for the advertised Ruby version
# 2. the method may be defined but "shouldn't" be for that version
# 3. the method may not be defined and shouldn't be defined
# The conditional needs to account for these cases. If the method is not
# defined but should be for that Ruby version, that should be a hard error.
# If the method is defined but shouldn't be for that Ruby version, it may
# not be a compatible definition and that should be checked early rather
# than resulting in a confusing error later. Finally, if it's not defined
# and shouldn't be, it's probably safe to define it.
#
# NOTE: Extra special care must be taken when defining a method that is
# "not" defined (eg could be private) for that Ruby version in case it
# over-rides an already defined engine-specific method.
#
# Pseudo-ish code
if expected_version and not expected_method
error "method expected for version"
elsif not expected_version and expected_method
if not check_expect_method_is_compatible
error "expected method is not compatible"
end
else
define_method
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment