using a hash in eval

F

Florian Weber

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
 
M

Markus

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'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
 
A

Ara.T.Howard

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

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
--
===============================================================================
| 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
===============================================================================
 
N

nobu.nokada

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
 
F

Florian Weber

module_eval <<-"end_eval", __FILE__, __LINE__
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..
 
F

Florian Weber

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?
 
N

nobu.nokada

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".
 
F

Florian Weber

#{foo}_method_which_expects_a_hash(options)
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top