Sippet needed to convert from a string

T

Tom Reilly

Snippit needed

given some option variables

c1 = 0
c2 = 0
c3 = 0
c4 = 0

given a string (a set of pairs of options) which could be read from
MYSQL or a file

paramString = '"c1" 0 "c3" 1 "c2" "y" "c4" "v"'

such that:

c1 = 0
c2 = "y"
c3 = 1
and c4 = "v"

at the end of the procedure.

I just can't figure out how to do this with RUBY.

Thanks

Tom Reilly
 
L

Lyndon Samson

------=_Part_71816_28983166.1136610240641
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Snippit needed

given some option variables

c1 =3D 0
c2 =3D 0
c3 =3D 0
c4 =3D 0

given a string (a set of pairs of options) which could be read from
MYSQL or a file

paramString =3D '"c1" 0 "c3" 1 "c2" "y" "c4" "v"'

such that:

c1 =3D 0
c2 =3D "y"
c3 =3D 1
and c4 =3D "v"


eval is your friend for these type of tasks.

------=_Part_71816_28983166.1136610240641--
 
W

Wilson Bilkovich

Snippit needed

given some option variables

c1 =3D 0
c2 =3D 0
c3 =3D 0
c4 =3D 0

given a string (a set of pairs of options) which could be read from
MYSQL or a file

paramString =3D '"c1" 0 "c3" 1 "c2" "y" "c4" "v"'

such that:

c1 =3D 0
c2 =3D "y"
c3 =3D 1
and c4 =3D "v"

at the end of the procedure.

I just can't figure out how to do this with RUBY.

There's no way yet to "meta-programmatically" set local variables. If
you're OK with instance variables, you could replace the code I'm
about to paste with calls to instance_variable_set.

Here's one way, using a Hash:

irb(main):001:0> require 'enumerator'
=3D> true
irb(main):002:0> params =3D "c1 0 c3 1 c2 y c4 v"
=3D> "c1 0 c3 1 c2 y c4 v"
irb(main):003:0> h =3D {}
=3D> {}
irb(main):004:0> params.split.each_slice(2) {|key, val| h[key] =3D val}
=3D> nil
irb(main):005:0> h
=3D> {"c1"=3D>"0", "c2"=3D>"y", "c3"=3D>"1", "c4"=3D>"v"}
irb(main):006:0>
 

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
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top