Using a hash in eval

hi!

is there a better way to do this:

options = <<-END_OF_OPTIONS
  #{options.inspect}
END_OF_OPTIONS
options.gsub!("\\\"", "'")

module_eval <<-"end_eval", __FILE__, __LINE__
   def foo
     some_method_which_expects_a_hash(#{options})
   end
end_eval

i guess iterating over each key and value and writing it 'by hand' is
the only way, right?

thanks!

ciao!
flroian

I'm not exactly sure what you are trying to do. Do any of these do what
you want? And by "better" are you meaning faster, easier to maintain,
more robust, or...?

options = eval(options.inspect.gsub(/[\]{3}/,"'"))

or

options.each_key { |k| options[k].gsub(/[\]{3}/,"'") }

or

temp = {}
options.each_pair { |k,v|
    temp[k.gsub(/[\]{3}/,"'")] = v.gsub(/[\]{3}/,"'")
    }
options = temp

Also, why do you need to jump through hoops to define foo? Is "options"
going to change later?

-- Markus

···

On Wed, 2004-09-08 at 10:16, Florian Weber wrote:

hi!

is there a better way to do this:

options = <<-END_OF_OPTIONS
  #{options.inspect}
END_OF_OPTIONS
options.gsub!("\\\"", "'")

module_eval <<-"end_eval", __FILE__, __LINE__
   def foo
     some_method_which_expects_a_hash(#{options})
   end
end_eval

some_method_which_expects_a_hash(YAML::load("#{ YAML::dump options }"))

or even

    some_method_which_expects_a_hash(Marshal::load("#{ Marshal::dump options }"))

maybe.

-a

···

On Thu, 9 Sep 2004, Florian Weber wrote:

hi!

is there a better way to do this:

options = <<-END_OF_OPTIONS
  #{options.inspect}
END_OF_OPTIONS
options.gsub!("\\\"", "'")

module_eval <<-"end_eval", __FILE__, __LINE__
  def foo
    some_method_which_expects_a_hash(#{options})
  end
end_eval

i guess iterating over each key and value and writing it 'by hand' is
the only way, right?

thanks!

ciao!
flroian

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
A flower falls, even though we love it;
and a weed grows, even though we do not love it. --Dogen

===============================================================================

Hi,

At Thu, 9 Sep 2004 02:16:56 +0900,
Florian Weber wrote in [ruby-talk:111898]:

i guess iterating over each key and value and writing it 'by hand' is
the only way, right?

  define_method(:foo)
    some_method_which_expects_a_hash(options)
  end

···

--
Nobu Nakada

module_eval <<-"end_eval", __FILE__, __LINE__
   def foo
     some_method_which_expects_a_hash(#{options})
   end
end_eval

I'm not exactly sure what you are trying to do. Do any of these do what
you want? And by "better" are you meaning faster, easier to maintain,
more robust, or...?

well, basically just not so breakable. inspecting and then replacing \" just
doesnt sound so safe..

At Thu, 9 Sep 2004 02:16:56 +0900,
Florian Weber wrote in [ruby-talk:111898]:

i guess iterating over each key and value and writing it 'by hand' is
the only way, right?

  define_method(:foo)
    some_method_which_expects_a_hash(options)
  end

that unfortunately doesn't really work in my situation,
because i'm accessing some methods and variables
like:

#{foo}_method_which_expects_a_hash(options)

is there no other way to do that?

Hi,

At Mon, 13 Sep 2004 05:54:34 +0900,
Florian Weber wrote in [ruby-talk:112351]:

that unfortunately doesn't really work in my situation,
because i'm accessing some methods and variables
like:

#{foo}_method_which_expects_a_hash(options)

is there no other way to do that?

  foo = method("#{foo}_method_which_expects_a_hash")
  define_method(:foo) do
    foo.call(options)
  end

# I forgot "do".

···

--
Nobu Nakada

#{foo}_method_which_expects_a_hash(options)

is there no other way to do that?

  foo = method("#{foo}_method_which_expects_a_hash")
  define_method(:foo) do
    foo.call(options)
  end

# I forgot "do".

awesome! thanks a lot!

i was wondering, are there any differences concerning
performance when the method is called.. or is it the same
wether the method is created with eval or with define_method?