user3799793
user3799793

Reputation: 131

Ruby the hard way example 39

I've tried my best understand how each line of the code works, but it seems I'm just getting more lost as I go. I know http://ruby.learncodethehardway.org/book/ex39.html explains the functions but I'm getting lost on the actual code. An example would be how k, v = kv works for the get slot function. May I please get a detailed explanation at on how the iterating variable work in this code?

module Dict
  def Dict.new(num_buckets=256)
    # Initializes a Dict with the given number of buckets.

    # As far as i can tell for this bit of code is that it creates an array called aDicts what will store 255 separate arrays
    # and each time a new Dict is inialized it will create an array that holds 255 arrays
    aDict = []
    (0...num_buckets).each do |i|
      aDict.push([])
    end

    return aDict
  end

  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.

    #takes the key arguments and converts it into a hash number that is divided by the length
    #of the Dict. The remainder a number on the aDict.
    return key.hash % aDict.length
  end

  def Dict.get_bucket(aDict, key)
    # Given a key, find the bucket where it would go.

    #Takes the Dict.hash_key functions and stores it in bucket_id
    #I am guessing this allows the assigned of hash_key to an index of aDict? Not to sure
    bucket_id = Dict.hash_key(aDict, key)
    return aDict[bucket_id]
  end

  def Dict.get_slot(aDict, key, default=nil)
    # Returns the index, key, and value of a slot found in a bucket.

    #Gets the bucket_id/index or index value and stores it in bucket
    bucket = Dict.get_bucket(aDict, key)
    #I honestly am lost how this iterator works especiallys on whats going on with the varibles
    bucket.each_with_index do |kv, i|
      k, v = kv
      if key == k
        return i, k, v
      end
    end
    #No clue on where the -1 comes it
    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.

    #Im lost here to
    i, k, v = Dict.get_slot(aDict, key, default=default)
    return v
  end

  def Dict.set(aDict, key, value)
    # Sets the key to the value, replacing any existing value.

    #little understanding of how the rest of this code works 
    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

  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

  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

Upvotes: 0

Views: 298

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

if you have an array, like

name = ["John", "Doe"]

you can do what's called "multiple assignment" which takes multiple elements on the right and assigns them to multiple variables on the left.

first_name, last_name = name

p first_name
=> "John"

p last_name
=> "Doe"

The line

k, v = kv

is that sort of multiple assignment. kv contains multiple values

We can see this in the Dict.set method...

if i >= 0
  bucket[i] = [key, value]
else
  bucket.push([key, value])
end

So clearly each individual bucket entry is an array of key and value.

Upvotes: 1

Related Questions