I'm trying to write an internal DSL in Ruby and am running into trouble with implementing a particular syntax. Specifically, if I have an array (or hash) in a class, I'd like to access and edit it with parentheses. For example:
class MyData
attr_accessor :things
def initialize
@things = ['right', 'right', 'wrong']
end
end
test = MyData.new
# This obviously won't work, but it's the syntax that I want.
test.things(2) = 'right'
I know I can read an element with this syntax by doing:
class MyData
def things(index)
@things[index]
end
end
test = MyData.new
test.things(2) # => 'wrong'
but actually changing the element is another thing entirely because Ruby just doesn't know what to do with that assignment operator.
The reason I want this strange syntax is because I hope I can use this language to easily convert some Fortran namelist files into a friendly Ruby environment, and Fortran unfortunately indexes arrays with parentheses.
My fear is that this is just one of those situations where I'm trying too hard to make Ruby go against its core syntax and I'll need to actually write a parser. Any thoughts?