Skip to content

Instantly share code, notes, and snippets.

@the-spectator
Created February 7, 2020 15:25
Show Gist options
  • Save the-spectator/bd2d68ba1dcb548ca1c2d08970cd719f to your computer and use it in GitHub Desktop.
Save the-spectator/bd2d68ba1dcb548ca1c2d08970cd719f to your computer and use it in GitHub Desktop.
Closure memeonization in ruby, It's magical
# Closure memeonization in ruby
# It's magical
Role::TYPES.each do |key, name|
puts "before nil assignment #{ivar.object_id}" rescue "------"
ivar = nil
puts ivar.object_id, ivar
define_method(:"#{key}_role") do
puts "Inside Role: #{ivar.object_id}"
ivar ||= Role.find_or_create_by(name: name, is_active: true)
puts "Inside Role (after): #{ivar.object_id}"
end
puts "================================"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment