metaprogramming - Dynamically creating accessors in Ruby module -


I have a config module in Ruby which I want to be able to add to the arbitrary variable. I made it as follows using method_missing and example_variable_set:

  module Conf # configure Sir DRF self.method_missing (M * Arges) methods arbitrarily args = args.pop = Args.length = = 1 instance_variable_set ("@ # {m}", args) and end   

However, I have trouble creating dynamically the accessers when I use attr_accessor as follows I am:

  module config # configured Sir DRF self.method_missing (m, * args) args = args.pop If arbitrary methods are found in args.length == 1   

I get the following:

Conf: "# # {m}" ("@ # {m} "Arges) Modul_avl (" attr_accessor: # {m} ") end end : S3_key ( '1234ABC') #Conf :: s3_key = nil

and if I make separate Aksesrs I try: module con # # def self.method_missing (m, * args) args = args.pop if args.length == 1 example_v_y_set ("@ # {m}" , Args) module_evale ("def self. # {M}; @ # {M}; End ") module_Aval (" def self. # {M} = (val); @ # {m} = val; end ") End:

is:

  conf :: s3_key ('1234ABC') # Conf :: s3_key = '1234ABC' - true   

But if I try to overwrite the value then

itemprop = "text">

first, attr_accessor is unusable for the module, have been even more common as described.

  module Conf Atr_srs Second, the error of overwriting is because  method_mission  is executed only once  
  def self.method_missing (m, * args) #: instance_variable_set ( "@ # {m}", Arges) Modul_avl ( "def self. # {M}; @ # {m}; end") # & lt; - method defined   

The method is defined in the first call and the number of arguments 0

  conf :: s3_key ('1234 ABC') #Call method_mission config :: s3_key ('4567DEF') # call itself .3_key ()   

For example, how about:

  module Conf Diarf self.method_missing (m, * args) args = args.pop If args.length == 1 Udahrn_wyay_set (# {M} (* Arges) if (args.empty?) @ # {M} and @ # {M} = (Args), "@ # {m}", Args) module_evale (& lt; & Lt; Length == 1)? Arg3.pop: Args End and EOS End Conf: s3_key ('foo') Conf: s3_key ('bar') p Conf :: s3_key # "bar"   

or < / P>

  module config DRF self.method_missing (m, * args) if (m.to_s = ~ / ^ (++) = $ /) args = args.pop if args.length == 1 Instance_variable_set ( "@ # {$ 1}", Arges) else instance_variable_get ( "@ # {m}") end end end Conf :: s3_key = 'foo' Conf :: s3_key = 'bar' p Conf :: s3_key # " Bar "   

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -