正在添加断言函数



我是RubyonRails的新手,现在正在经历"艰难地学习Ruby"。在本课中,我将向Dict.rb添加Assert功能(如下所示)。有人知道我该怎么做吗?每次我尝试时都会收到一个错误。

module Dict
    #Creates a new function that makes a Dictionary. This is done through creating the 
    # aDict variable that has an array in which num_buckets array is placed inside.
    # These buckets will be used to hold the contents of the Dict and later aDict.length
    # is used to find out how many buckets there are.  
    def Dict.new(num_buckets=256)
        # Initializes a Dict with the given number of buckets.
        aDict = []
        (0...num_buckets).each do |i|
            aDict.push([])
        end
        return aDict
    end
    # Converts a string to a number using the bult-in Ruby 'hash' function
    # Once I have a number for the key, I use the % operator and aDict.length to get a 
    # bucket where the remainder can go. 
    def Dict.hash_key(aDict, key)
        # Given a key this will create a number and then convert it to an index for the
        # aDict's buckets
        return key.hash % aDict.length
    end
    # Uses hash_key to find a bucket that the key could be in. Using bucket_id I can get 
    # the bucket where the key could be. By using the modulus operator I know it will fit
    # into the aDict array of 256.
    def Dict.get_bucket(aDict, key)
        # Given a key, find the bucket where it would go.
        bucket_id = Dict.hash_key(aDict, key)
        return aDict[bucket_id]
    end
    # Uses get_slot to get the (i, k, v) and returns the v (value) only. 
    def Dict.get_slot(aDict, key, default=nil)
        # Returns the index, key and value of a slot found in a bucket.
        bucket = Dict.get_bucket(aDict, key)
        bucket.each_with_index do |kv, i|
         k, v = kv
         if key == k
            return i, k, v
         end
        end
        return -1, key, default
    end
    def Dict.get(aDict, key, default=nil)
        # Gets the value in a bucket for the given key or the default.
        i, k, v = Dict.get_slot(aDict, key, default=default)
        return v
    end
    # Sets a key/value pair by getting the bucket and appending the new (key, value) to it.
    # First you have to get the bucket, see if the key already exists, if it does then
    # replace it, if it doesn't get replaced then append it. 
    def Dict.set(aDict, key, value)
        # Sets the key to the value, replacing any existing value.
        bucket = Dict.get_bucket(aDict, key)
        i, k, v = Dict.get_slot(aDict, key)
        if i >= 0
            bucket[i] = [key, value]
        else
            bucket.push([key, value])
        end
    end
    # Deletes a key by getting the bucket, searching for key in it and deleting it form the
    # array. 
    def Dict.delete(aDict, key)
        # Deletes the given key from the Dict.
        bucket = Dict.get_bucket(aDict, key)
        (0...bucket.length).each do |i|
            k, v = bucket[i]
            if key == k
                bucket.delete_at(i)
                break
            end
        end
    end
    # goes through each slot in each bucket and prints out what's in the Dict. 
    def Dict.list(aDict)
        # Prints out what's in the Dict.
        aDict.each do |bucket|
            if bucket
                bucket.each {|k, v| puts k, v}
            end
        end
    end
end

我正在使用以下脚本从模块Dict.rb:运行方法

require './dict.rb'
# create a mapping of state to abbreviation
states = Dict.new()
Dict.set(states, 'Oregon', 'OR')
Dict.set(states, 'Florida', 'FL')
Dict.set(states, 'California', 'CA')
Dict.set(states, 'New York', 'NY')
Dict.set(states, 'Michigan', 'MI')
# create a basic set of states and some cities in them
cities = Dict.new()
Dict.set(cities, 'CA', 'San Francisco')
Dict.set(cities, 'MI', 'Detroit')
Dict.set(cities, 'FL', 'Jacksonville')
# add some more cities
Dict.set(cities, 'NY', 'New York')
Dict.set(cities, 'OR', 'Portland')
# puts out some cities
puts '-' * 10
puts "NY State has: #{Dict.get(cities, 'NY')}"
puts "OR State has: #{Dict.get(cities, 'OR')}"
# puts some states
puts '-' * 10
puts "Michigan's abbreviation is: #{Dict.get(states, 'Michigan')}"
puts "Florida's abbreviation is: #{Dict.get(states, 'Florida')}"
# do it by using the state then cities dict
puts '-' * 10
puts "Michigan has: #{Dict.get(cities, Dict.get(states, 'Michigan'))}"
puts "Florida has: #{Dict.get(cities, Dict.get(states, 'Florida'))}"
# puts every state abbreviation
puts '-' * 10
Dict.list(states)
# puts every city in state
puts '-' * 10
Dict.list(cities)
puts '-' * 10
# by default ruby says "nil" when something isn't in there
state = Dict.get(states, 'Texas')
if !state
    puts "Sorry, no Texas."
end
# default values using ||= with the nil result
city = Dict.get(cities, 'TX', 'Does Not Exist')
puts "The city for the state 'TX' is: #{city}"

Ruby中默认没有"assert"方法。这就是为什么你会得到一个"未定义的方法"错误。您要么需要自己实现这个方法,要么使用Minitest或Rspec之类的测试框架。

如果你想自己实现一个简单的断言方法,它应该是这样的:

def assert(first_item, second_item)
  unless first_item == second_item
    puts "[Error] #{first_item} does not equal #{second_item}"
  end
end

然后像这样使用:

assert(Dict.get(cities, 'NY'), 'New York')

其思想是断言可以帮助您确保代码返回预期的输出,而无需手动检查。

最新更新