If I have an OpenStruct:
require 'ostruct'
open_struct = OpenStruct.new
I can overwrite [] which works in some cases
open_struct.define_singleton_method(:[]) do |*args|
puts args.map(&:class)
puts args
end
open_struct.a = 1
open_struct[:a]
# => Symbol
# a
But this [] method is not called when using the dot-method syntax:
open_struct.a
# => 1
I am trying to make a class which inherits from OpenStruct and works more like a Javascript object (basically I'm trying to remove the necessity to run call on a proc that's stored as a value)
First of all - OpenStruct already functions very much like JavaScript (given that
#[]is a synonym of#call):JS:
Ruby:
If you mean function more like Ruby... you can override
new_ostruct_member:Just be aware that
OpenStructin Ruby slows down your program, and shouldn't be used if you can avoid it.