Array hashes in Ruby?

For general discussion related FlowStone
Post Reply
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Array hashes in Ruby?

Post by kortezzzz »

Hi guys,

Here is a little ruby challenge I'm trying to deal with:

I would like to use hashes to define string values just like the "if then" primitive does, but unlike with the standard ruby hashes code where the whole defining process done inside the ruby primitive, I would like to define it externally, by feeding the code with 2 random string arrays. The first array would be used as a "variable to be compared" list and the second would be used as a "condition" list.

So, the ruby code should have 3 inputs and one output. The inputs would be:
1) "user" input
2) "variable to be compared" input
3) "condition" input

The output would be just the "final result".

Each index in the first array is actually equivalent to the same index int second array, so for instance if the arrays are:

a
b
c

1
2
3

it means that a=1,b=2,c=3. Therefore, if the user's string input is "cab" the array would 3,1,2 so the "final result" would be:

3
1
2

Also attached a small diagram schematic that describes the wanted structure.
Thanks :)
Attachments
(ruby arrays if then).fsm
(502 Bytes) Downloaded 888 times
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Array hashes in Ruby?

Post by KG_is_back »

Code: Select all

h=Hash.new
@ins[1].zip(@ins[2]).each{|(k,v)| h[k]=v}

output 0,@ins[0].map{|k| h[k]}


Note: the code is not idiot-proof. If the "hash arrays" are not of equal size, errors may occur.
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Array hashes in Ruby?

Post by kortezzzz »

Brilliant KG :D
Thank you so much. So far, works perfectly with no issue :)
Post Reply