Looks to me like you will have an Array of hashes.

The ruby way something like a single line array select

 t.select{|tt| a.between?(tt[:min], tt[:max]) && b.between?(tt[:b_min], 
tt[:b_max])}

create the array initially with something like

t = Array.new
t << {a_min: 1, a_max:10, b_min: 100, b_max: 1000, p1: 1, p2: 2, p3: 3} 
t << {a_min: 1, a_max:10, b_min: 100, b_max: 1000, p1: 1, p2: 2, p3: 3}  

(fill in your data)

you could do t = [{your data}, {your hash data}….]

If you only want the first value in the array that matches, use t.detect rather 
than t.select

It will return either any array of matching hashes or with detect the first one…


Use the rails console to test all this stuff.  rails c




> On Dec 16, 2015, at 3:55 PM, Suo Huang <suo.huang...@gmail.com> wrote:
> 
> Hi all,
> 
> I am a newbie in Ruby, and I am having a small question with the hash table. 
> If anyone could give me some suggestions that would be great!
> 
> Considering creating a hash table like this:
> 
>  a_Min        a_Max     b_Min          b_Max  p1      p2          p3          
>   p4             p5          p6
> 40.000        47.95   -95.0           -74.01  12.8     6.5      177.82        
>     0.9150           68.62          0.9020
> 42.000        47.95   -68.0   -59.50  11.0     5.8      185.20            
> 0.9377           -11.80         0.9538
> 44.000        47.95   -74.2   -68.05  12.8  6.5         157.45            
> 0.9194           37.55          0.9297
> 46.000        66.00   -59.5   -51.00  8.8     3.7       164.96            
> 0.9465           -207.54        1.0342
> 47.951        66.00   -79.0   -59.55  12.8    6.5       157.45            
> 0.9194           37.55          0.9297
> 47.951        66.00   -95.0   -79.01  12.8    6.5       177.82            
> 0.9150           68.62          0.9020
> ...
> 
> If giving any (a,b), it will fall within the four boundary control values in 
> the hash table (a_Min, a_Max, b_min, b_Max) and returns its relative 
> parameters (p1, p2, p3....)
> 
> I am trying something like this:
> 
> def functionXXX(a, b)
>     threshold = XXX                                      # how to create a 
> hash table with the above data?
>     for t in threshold do     
>           if (t[:a_Min]<=a && a<t[:a_Max] && t[:b_Min]<=b && b<t[:b_Max]) 
> then    #if there is a smarter way in Ruby for this?
>              return t
>              break
>           end
>     end
> end
> 
> Would this be the correct way of doing this task in Ruby?
> 
> Thanks!
> 
> Suo
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to rubyonrails-talk+unsubscr...@googlegroups.com 
> <mailto:rubyonrails-talk+unsubscr...@googlegroups.com>.
> To post to this group, send email to rubyonrails-talk@googlegroups.com 
> <mailto:rubyonrails-talk@googlegroups.com>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/rubyonrails-talk/97f8d9fe-4632-49cd-a5d7-aeb80da4472b%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/rubyonrails-talk/97f8d9fe-4632-49cd-a5d7-aeb80da4472b%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/C636E51E-A00E-4F11-B777-1FD6F8A75F51%40btrtrucks.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to