Searching through an array using Ruby's array.index() method using wildcards -


i have created array of tiles game board. each element of array hash following format:

{'x' => x, 'y' => y, 'base' => "base"} 

the value of 'base' may string no spaces.

i find index of particular tile based on x/y values of tile, regardless of tile's base value.

my first thought on how accomplish search contents of array using index() method, this:

active_tile = tile.index({'x' => 3, ''y' => 2, 'base' => "wildcard_here" }) 

however, have no idea how implement wildcard this. suggestions appreciated.

i appreciate suggestions better methods of storing , retrieving tiles if knows method i'm using not effective one.

for completeness, here entire piece of code i'm working right now:

class map   attr_reader :max_ns   # maximum north-south size   attr_reader :max_ew   # maximum east-west size   attr_reader :tile     # array of tiles    def initialize(tall, wide)     @max_ns = tall     @max_ew = wide      # create array of tiles x/y coordinates , tile base type.     @tile = []     (1..tall).each |y|       (1..wide).each |x|         @tile.push({'x' => x, 'y' => y, 'base' => "ocean"})       end     end      # pick spot start continent.     rand_y = rand(3..tall - 2)     rand_x = rand(3..wide - 2)     continental_base = @tile.index({'x' => rand_x, 'y' => rand_y, 'base' => "ocean"})     @tile[continental_base]['base'] = "land"      # spiral around spot, creating larger land mass     # finish writing function grab array indices of tile's neighbors first.   end    # function find array indices of tiles neighboring given tile.   def tile_neighbors(x, y)     # first index of tile given x & y coordinates.     cur_tile = @tile.index({'x' => x, 'y' => y, 'base' => "/\a(...)\z/"})   end end 

yes, using array#index sensible:

arr = [{x: 1, y: 2, base: "yo, how ya' doin'?"},        {x: 1, y: 3, base: "hey, bro!"},        {x: 2, y: 5, z: 4 }]  target = { x: 1, y: 2 } arr.index { |h| h.values_at(*target.keys) == target.values } #=> 0  target = { x: 1, y: 3 } arr.index { |h| h.values_at(*target.keys) == target.values } #=> 1  target = { y: 5, x: 2 } arr.index { |h| h.values_at(*target.keys) == target.values } #=> 2  target = { x: 2, y: 'cat' } arr.index { |h| h.values_at(*target.keys) == target.values } #=> nil 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -