Created
December 20, 2011 13:48
-
-
Save pbosetti/1501618 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
ary = ["a='test'", "b=10", "c=[1,2,3]"] | |
class MyParser | |
def self.[](ary) | |
mp = self.new | |
ary.each do |e| | |
if e =~ /^(\w+)\s*=/ then | |
attr_accessor $1.to_sym | |
mp.instance_variable_set("@#{$1}", eval(e)) | |
end | |
end | |
return mp | |
end | |
end | |
# per catturare tutte le variabili all'interno dell'oggetto | |
mp = MyParser[ary] | |
p [mp.a, mp.b, mp.c] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment