Coder Social home page Coder Social logo

programming-univbasics-4-basic-hashes-lab's Introduction

Intro to Hashes Lab

Learning Goals

  • Build simple hashes.

Introduction

It's time to practice what we've learned about Ruby hashes. In this lab, your task is to implement a set of three methods. Each method will require you to complete some basic hash creation.

Instructions

Code your solutions for the following four methods in the lib/intro_to_ruby_hashes_lab.rb file.

  1. Implement a method called new_hash that creates and returns a new, empty hash.

  2. Implement a method called my_hash that returns a valid hash. This hash should have at least one key/value pair of your choice.

  3. Implement a method called pioneer that returns a hash. This hash should have a key that is a symbol :name and whose value is a string, 'Grace Hopper'.

  4. Implement a method called id_hash_generator that takes in one parameter, an integer. This method should create and return a hash with a key :id with the integer as the value.

Try your best to solve these. Use learn and follow the test suite messages as you work through your solution and learn submit when you're finished with your work. Check out the walkthrough below if you get stuck.

Solving this Lab

The four methods we'll be working in are already provided in lib/intro_to_ruby_hashes.rb, so all we need to do is write the implementation for each.

The first method, new_hash, should return a new, empty hash. The most direct way to do this is to write a hash in its implicit form:

def new_hash
  {}
end

Alternatively, we could explicitly create a new Hash:

def new_hash
  Hash.new
end

But we don't see this as often.

When first learning to work with hashes, it is common to think we always need to assign it to a variable, so your solution might look like this:

def new_hash
  hash = {}
  hash
end

Or even this:

def new_hash
  hash = {}
end

While both of these are valid code and return an empty hash, the variable is not needed. All that matters is what gets returned. Because Ruby methods use implicit return, when we create a hash using {}, we can return that directly.

For the second method, my_hash, we will also return a hash. This hash must have some key/value pair included. Your choice.

def my_hash
  { :favorite_color => "blue" }
end

When using symbols for keys, we can write key/value pairs as seen above, or with the alternative syntax:

def my_hash
  { favorite_color: "blue" }
end

For the third method, pioneer, the task is the same as the previous method. Except this time, the key should be :name and the value should be Grace Hopper:

def pioneer
  { name: "Grace Hopper" }
end

In the final method, id_hash_generator, we need to use a parameter. This parameter should be assigned to the :id key in a hash, and the hash should be returned. Similar to the previous methods, we return a hash, giving it one key. The value will be set to whatever the parameter is:

def id_hash_generator(number)
  { id: number }
end

Conclusion

Hashes are an essential data type that we will use frequently. Now that you've practiced the basics of creating hashes, we will practice reading and updating them.

Resources

programming-univbasics-4-basic-hashes-lab's People

Contributors

maxwellbenton avatar sophiedebenedetto avatar msuzoagu avatar deniznida avatar victhevenot avatar ahimmelstoss avatar aviflombaum avatar fs-lms-test-bot avatar pletcher avatar roseweixel avatar sarogers avatar octosteve avatar fislabstest avatar ehawk823 avatar ga-be avatar markedwardmurray avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.