Skip to content

Instantly share code, notes, and snippets.

@dnesteryuk
Created January 10, 2020 16:25
Show Gist options
  • Save dnesteryuk/ced55d30ccc73e704ef3c25ddc1180c5 to your computer and use it in GitHub Desktop.
Save dnesteryuk/ced55d30ccc73e704ef3c25ddc1180c5 to your computer and use it in GitHub Desktop.
require 'ostruct'
require 'benchmark/ips'
class User
attr_accessor :name, :email
def initialize(attrs)
attrs.each { |key, val| send("#{key}=", val) }
end
end
Benchmark.ips do |x|
x.report('class with accessors') { User.new(name: 'John', email: 'john@example.org') }
x.report('OpenStruct') { OpenStruct.new(name: 'John', email: 'john@example.org') }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment