Created
February 7, 2020 15:25
-
-
Save the-spectator/bd2d68ba1dcb548ca1c2d08970cd719f to your computer and use it in GitHub Desktop.
Closure memeonization in ruby, It's magical
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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