Skip to content

Instantly share code, notes, and snippets.

@pbosetti
Created December 20, 2011 13:48
Show Gist options
  • Save pbosetti/1501618 to your computer and use it in GitHub Desktop.
Save pbosetti/1501618 to your computer and use it in GitHub Desktop.
#!/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