Object

Hi,

i have the following script:

werte = “anfang=123&ende=was”

werte.split(/&/).each do |x|
key, val = x.split(/=/,2)
puts "key = #{key} , val = #{val}"
end

puts anfang # => 123

The goal is when i make puts anfang to get the value “123”.
The problem is the values from werte can be changed
for example:
werte = “irgendwas=hu&ruby=super”

Now i will make puts irgendwas => hu
And so on.

Is this possible?

Manfred

++ I’m not sure what you asked, but it seems to me that you:: ++

have a string $some_string which holds the values and the var name.
after that you get the key and the val,
and by the key you’d like to print the value.

You even want to change $some_string.

I think what you want to do could be possible using a simple hash like
werteHash={}
and putting each [key, val ] pair in that.
This way you could call your puts with

puts werteHash[key]

You can even add a singleton method to the Hash class to have a method
like
someHash.key_val_from_string(werte)

or simply let it as a method in main .

PS
what does werte mean? it is ~ “word” ? :slight_smile:

···

On Sat, 04 Jan 2003 20:04:20 +0100, Manfred Hansen manfred@toppoint.de wrote:

werte = “anfang=123&ende=was”

werte.split(/&/).each do |x|
key, val = x.split(/=/,2)
puts “key = #{key} , val = #{val}”
end

puts anfang # => 123

You could think of a better Subject here…

Manfred Hansen wrote:

i have the following script:

werte = “anfang=123&ende=was”

werte.split(/&/).each do |x|
key, val = x.split(/=/,2)
puts “key = #{key} , val = #{val}”
end

puts anfang # => 123

The goal is when i make puts anfang to get the value “123”.
The problem is the values from werte can be changed
for example:
werte = “irgendwas=hu&ruby=super”

Now i will make puts irgendwas => hu
And so on.

Is this possible?

Here’s the relevant code from MiniRubyWiki:

def decodeURL post
result = {}

post.split('&').each do
        >token>
        token = CGI::unescape(token)
        key, value = token.split(/=/, 2)
        result[key] = value
    end

return result

end

The outer splitter breaks on the & partitions, and the inner splitter
breaks on the =. Then the method returns everyone in one big happy map.

If you are using CGI, you’l need the ‘CGI::unescape’ to turn %20 into
space, etc.

···


Phlip
greencheese.org
– Got in trouble at StarBucks. I tried to order
“A double latte mocha and a body piercing.” –

I suppose that Philips CGI solution is what you are really after.
However, if you still want to create variables as in your example, here is
how:

Use the ‘eval’ function. A first thought is to type:

eval “#{key} = #{val}”

But this has the problem of scope. This is read as:
eval “anfang = ‘123’”

‘anfang’ is local to the eval, and it disappears when the eval is done.
The best you can do is to make it a global variable:

eval “$#{key} = #{val}”

Now $anfang exists after the eval is done, and it has the value ‘123’.

I hope you realize that, as it is, ‘123’ is a string, not an integer.
Perhaps what you want is:

eval “$#{key} #{val.to_i}”

Cheers,
Daniel.

···

Hi,

i have the following script:

werte = “anfang=123&ende=was”

werte.split(/&/).each do |x|
key, val = x.split(/=/,2)
puts “key = #{key} , val = #{val}”
end

puts anfang # => 123

The goal is when i make puts anfang to get the value “123”.
The problem is the values from werte can be changed
for example:
werte = “irgendwas=hu&ruby=super”

Now i will make puts irgendwas => hu
And so on.

Is this possible?

Manfred

werte are values .

mfg. Jonas Hoffmann

···

Am Samstag, 4. Januar 2003 18:16 schrieb gabriele renzi:

PS
what does werte mean? it is ~ “word” ? :slight_smile:

Hi,

Daniel Carrera wrote:

I suppose that Philips CGI solution is what you are really after.
However, if you still want to create variables as in your example, here is
how:
Yes and
I have start to write a mod_ruby handler. Here i am possible
to read the values from the “POST” and “GET” methods.
The values are in on string for example
werte = “anfang=123&ende=was”

At the moment i am possible to get the values over a
hash in the eruby scripts. For example:
puts “anfang = #{$hsh[‘anfang’]}”

I will be nice to do that without complicated hash
in eruby scripts. The same in php scripts.

Ok i will try this with eval.

Cheers,
Manfred

···

Use the ‘eval’ function. A first thought is to type:

eval “#{key} = #{val}”

But this has the problem of scope. This is read as:
eval “anfang = ‘123’”

‘anfang’ is local to the eval, and it disappears when the eval is done.
The best you can do is to make it a global variable:

eval “$#{key} = #{val}”

Now $anfang exists after the eval is done, and it has the value ‘123’.

I hope you realize that, as it is, ‘123’ is a string, not an integer.
Perhaps what you want is:

eval “$#{key} #{val.to_i}”

Cheers,
Daniel.

Hi,

i have the following script:

werte = “anfang=123&ende=was”

werte.split(/&/).each do |x|
key, val = x.split(/=/,2)
puts “key = #{key} , val = #{val}”
end

puts anfang # => 123

The goal is when i make puts anfang to get the value “123”.
The problem is the values from werte can be changed
for example:
werte = “irgendwas=hu&ruby=super”

Now i will make puts irgendwas => hu
And so on.

Is this possible?

Manfred