I’m trying to create a mixin module, Parser, that will allow me to do the following :
class MyParser
include Parser
field :my_field, 1, 10
field :my_other_field, 11, 15
end
m = MyParser.new("1234567890abcde")
m.my_field # - > "1234567890"
m.my_other_field # -> "abcde"
I’m new to meta-programming in ruby
Here are my questions ?
I need to create a @fields array, for each class that includes Parser, how do I do that
I want a field class method that can add new fields to the @fields array, how can I access a instance variable from a class_method?
How can I get the MyParser.new method to work as described ?
Thanks
Here it is. if you have trouble understanding the code, let me know and I will try to clarify it for you.