This file contains hidden or 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
| require 'benchmark/ips' | |
| require 'active_support/core_ext/hash' | |
| def sample | |
| { a: 1, b: 2, c: 3, d: 4, c: 5, d: 6, e: 7, f: 8, g: 9, h: 10 } | |
| end | |
| Benchmark.ips do |x| | |
| x.report 'map + to_h' do | |
| h = sample |
This file contains hidden or 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
| module Imageable | |
| def self.included(model_class) | |
| model_class.has_many :images, polymorphic: true | |
| model_class.extend ClassMethods | |
| end | |
| module ClassMethods | |
| def has_one_image(*names) | |
| names.each do |image_kind| | |
| define_method image_kind do |
This file contains hidden or 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
| module Imageable | |
| def self.build(methods) | |
| mod = Module.new do | |
| methods.each do |method| | |
| define_method method do | |
| images.find { |image| image.kind == 'main_image' } | |
| end | |
| end | |
| end |
This file contains hidden or 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
| class ModuleFactory < Module | |
| def initialize | |
| define_a_method | |
| end | |
| private | |
| def define_a_method | |
| module_eval do | |
| def my_method |
This file contains hidden or 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
| class Imageable < Module | |
| def initialize(has_one: %i(main_image)) | |
| has_one.each { |image_kind| define_image_getter(image_kind) } | |
| end | |
| private | |
| def included(model_class) | |
| model_class.has_many :images | |
| end |
This file contains hidden or 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
| class Project | |
| include Imageable.new(has_one: %i(main_image secondary_image)) | |
| end | |
| project = Project.new | |
| project.main_image #=> <Image kind="main_image" imageable_type="Project"> | |
| project.secondary_image #=> <Image kind="secondary_image" imageable_type="Project"> |
This file contains hidden or 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
| class Imageable < Module | |
| def initialize(has_one: %i(main_image)) | |
| has_one.each do |image_kind| | |
| define_method image_kind do | |
| images.find { |image| image.kind == 'main_image' } | |
| end | |
| end | |
| end | |
| def included(model_class) |
This file contains hidden or 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
| class ModuleGenerator < Module | |
| def build_methods(methods) | |
| module_eval do | |
| methods.each do |method| | |
| define_method(method) do | |
| puts "#{method} was defined in the instance methods" | |
| end | |
| end | |
| end | |
| end |
This file contains hidden or 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
| module OneTwoThree | |
| def one | |
| puts 'one was defined as an instance method' | |
| end | |
| def two | |
| puts 'two was defined as an instance method' | |
| end | |
| def three |
This file contains hidden or 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
| OneTwoThree = Module.new do | |
| def one | |
| puts 'one was defined as an instance method' | |
| end | |
| def two | |
| puts 'two was defined as an instance method' | |
| end | |
| def three |