# Sippet needed to convert from a string

**URL:** <https://www.ruby-forum.com/t/sippet-needed-to-convert-from-a-string/51787>\
**Category:** Ruby\
**Created:** [January 7, 2006, 12:22am UTC](https://www.ruby-forum.com/t/sippet-needed-to-convert-from-a-string/51787 "2006-01-07T00:22:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomrobinson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@tomrobinson](https://www.ruby-forum.com/u/tomrobinson)\
**Post date:** [January 7, 2006, 12:22am UTC](https://www.ruby-forum.com/t/sippet-needed-to-convert-from-a-string/51787/1 "2006-01-07T00:22:26Z")

</div>

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 R.

---

<div class="post-metadata">

**Author:** ![tomrobinson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@tomrobinson](https://www.ruby-forum.com/u/tomrobinson)\
**Post date:** [January 7, 2006, 6:05am UTC](https://www.ruby-forum.com/t/sippet-needed-to-convert-from-a-string/51787/2 "2006-01-07T06:05:02Z")

</div>

On 1/7/06, Tom R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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”

eval is your friend for these type of tasks.

---

<div class="post-metadata">

**Author:** ![tomrobinson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@tomrobinson](https://www.ruby-forum.com/u/tomrobinson)\
**Post date:** [January 7, 2006, 8:08am UTC](https://www.ruby-forum.com/t/sippet-needed-to-convert-from-a-string/51787/3 "2006-01-07T08:08:22Z")

</div>

On 1/6/06, Tom R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> MYSQL or a file  
> 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’  
=\> true  
irb(main):002:0\> params = “c1 0 c3 1 c2 y c4 v”  
=\> “c1 0 c3 1 c2 y c4 v”  
irb(main):003:0\> h = {}  
=\> {}  
irb(main):004:0\> params.split.each\_slice(2) {|key, val| h[key] = val}  
=\> nil  
irb(main):005:0\> h  
=\> {“c1”=\>“0”, “c2”=\>“y”, “c3”=\>“1”, “c4”=\>“v”}  
irb(main):006:0\>

---

<div class="post-metadata">

**Author:** ![tomrobinson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/d78d45/32.png) [@tomrobinson](https://www.ruby-forum.com/u/tomrobinson)\
**Post date:** [January 7, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/sippet-needed-to-convert-from-a-string/51787/4 "2006-01-07T23:38:05Z")

</div>

Lyndon S. wrote:

> On 1/7/06, Tom R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

Thanks for the help. This is just what I needed to get unstuck.
