Ruby(Hard Way,Ex48)匹配数组到hash然后为struct赋值?

时间:2013-11-16 22:54:49

标签: ruby

我查看了关于struct,map,array&的Ruby文档中的所有内容。散列;但无论出于什么原因还没有抓住如何做到这一点。我有两个问题。

  1. 查看数组和散列是否有共同值的好方法是什么?
  2. 从哈希中,我怎样才能找到一个值并“自动”获取它的密钥(或反之亦然)并将它们包含在其他东西的新实例中(例如Pair.new)?
  3. 我看到了这个SO回复,但没有多大帮助...... Learn Ruby Hard Way ex. 48

    也看了这个,但它没有用& Zed说使用map func ... Using Ruby, what is the most efficient way to check if any key in a hash matches any values within an Array

    可在此处找到练习说明。 Ruby The Hard Way EX.48 Instructions

    我的代码(尝试#2080)

    class Lexicon
    
    Pair = Struct.new(:token, :key)
    
    def scan(stuff)
        @words = stuff.split(" ")
        return analyze
    end
    
    def analyze
    
        hash = { :direction => "north", :direction => "south", 
                :direction => "east", :direction => "west", :verb => "go",  
                                     :verb => "stop"}
    
        @words.map do |word|
            if word == hash[:direction]
            #i need something here that says if word matches a value in hash... 
            #assign matching key/value to a new instance of Pair
                Pair.new(word)
            else
                                *#puts to see if anything is sticking* 
                puts "Oh god its not working #{word}"
                puts Pair[]
                puts hash[:direction]
               end
           end
      end
    end
    
    a = Lexicon.new()
    a.scan("north mama jeffrey homie")
    

    TERMINAL

    $ ruby lexicon.rb
    Oh god its not working north
    #<struct Lexicon::Pair token=nil, key=nil>
    west
    
    Oh god its not working mama
    #<struct Lexicon::Pair token=nil, key=nil>
    west
    
    Oh god its not working Jeffrey
    #<struct Lexicon::Pair token=nil, key=nil>
    west
    
    Oh god its not working homie
    #<struct Lexicon::Pair token=nil, key=nil>
    west
    

    MY CODE#2081与上述相同,但

       hash.each do |k, v|
          if v == @words
          #i need something here that says if word matches a value in hash... 
          #assign matching key/value to a new instance of Pair
            Pair.new(k,v)
          else
            puts "Oh god its not working"
            puts Pair[]
            puts hash[:direction]
            puts @words
          end
       end
    

    TERMINAL

    Oh god its not working
    #<struct Lexicon::Pair token=nil, key=nil>
    ...
    ...
    

3 个答案:

答案 0 :(得分:2)

这里有几个问题。最重要的是,你的哈希不起作用;哈希是使用唯一键建立在键值对上的,因此您的哈希实际上与以下内容相同:

hash = { :direction => "west", :verb => "stop"}

您可能最好更换哈希中的键值对,如下所示:

hash = { "north" => :direction, "south" => :direction, 
        "east" => :direction, "west" => :direction, "go" => :verb,  
                             "stop" => :verb }

@words.map do |word|
  hash.keys.include?(word) ? Pair.new(hash[word], word) : Pair.new(:error, word)
end

答案 1 :(得分:0)

def have_common_value?(array, hash)
  hash.values.any? { |v| array.include? v }
end

def get_pair_from_value(hash, value)
  hash.find { |k,v| v == value }
end

答案 2 :(得分:0)

哈希每个值只能有一个唯一键,因此,部分问题是上面生成的哈希只会返回一个:direction和一个:location

这应该有助于让您更接近您所寻找的目标:

class Lexicon

  Pair = Struct.new(:token, :key)

  def scan(stuff)
    @words = stuff.split(" ")
    return analyze
  end

  def analyze
    hash = { :directions => { :north => "north", :south => "south", :east => "east", :west => "west" },
             :actions => { :verb => "go", :verb => "stop" } }

    @words.map do |word|
      if hash[:directions].values.include?(word)
        Pair.new(word)
      else
        puts "Oh god its not working #{word}"
        puts Pair[]
        puts hash[:directions]
      end
    end
  end
end

a = Lexicon.new()
a.scan("north mama jeffrey homie")