Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active January 8, 2020 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/744b0d114da82ac1f9c124be649421b2 to your computer and use it in GitHub Desktop.
Save havenwood/744b0d114da82ac1f9c124be649421b2 to your computer and use it in GitHub Desktop.
My refinement suggestion for a #ruby IRC question about monkey patching
module SizeSquared
[String, Enumerable].each do |this_class|
refine this_class do
def size_squared
size.abs2
end
alias length_squared size_squared
end
end
end
using SizeSquared
'ule'.length_squared
#=> 9
5.times.size_squared
#=> 25
[].size_squared
#=> 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment