Coder Social home page Coder Social logo

ruby-vs-js-hash-dictionary's Introduction

General Assembly Logo

Ruby Hashes (versus JavaScript dictionaries)

Instructions

Fork, clone, branch (training), bundle install

Objectives

By the end of this, students should be able to:

  • Constrast Ruby Hashes with JavaScript dictionaries.
  • Create a Ruby Hash using both the implicit ({}) and new constructors.
  • Assign a value to or modify a value in a Ruby Hash using a specified key.
  • Access a value in a Ruby Hash using a specified key.
  • Obtain an Array of keys from a Ruby Hash.
  • Explain why Symbols are preferred over Strings as keys in a Ruby Hash.

Introduction

In Ruby, "A Hash is a dictionary-like collection of unique keys and their values". In sharp contrast to JavaScript, Ruby Hashes are not the most general object in the language, but are instances of a specialized class for key/value storage.

Ruby Symbols

A Symbol is a sequence of characters that is stored at most once in any instance of the Ruby interpreter.

In Ruby, strings are compared a character at a time, but symbols are compared by object_id. This makes comparing symbols fast and therefore much more performant than strings when used as keys in a Hash.

Demonstration

Each of the following pairs of operations are equivalent:

> 'bob'.equal? 'bob'
=> false
> 'bob'.object_id == 'bob'.object_id
=> false
> 'bob'.eql? 'bob'
=> true
> 'bob' == 'bob'
=> true

But all of the following operations are equivalent:

> :bob.equal? :bob
=> true
> :bob.object_id == :bob.object_id
=> true
> :bob.eql? :bob
=> true
> :bob == :bob
=> true

We can create a symbol with arbitrary characters if we surround the characters with quotes (either :'<characters>' or '<characters>'.to_sym):

> :'&foo'.equal? '&foo'.to_sym
=> true

How does this compare to keys in JavaScript?

Creating Ruby Hashes

Let's look at different ways to create a Hash.

Demonstration

> consultant = {}
=> {}
> consultant = Hash.new
=> {}
> consultant = Hash.new('')
=> {}
> consultant[:surname]
=> ""
> consultant
=> {}
> consultant = Hash.new {|hash, key| hash[key] = ''}
=> {}
> consultant[:surname]
=> ""
> consultant
=> {:surname=>""}
> consultant = { given_name: 'Antony', surname: 'Donovan', height: 76}
=> {:given_name=>"Antony", :surname=>"Donovan", :height=>76}
> consultant[:weight] += 10
NoMethodError: undefined method ``+'' for nil:NilClass
from (pry):2:in ``__pry__''
> consultant = Hash.new(0).merge({ given_name: 'Antony', surname: 'Donovan', height: 76})
=> {:given_name=>"Antony", :surname=>"Donovan", :height=>76}
> consultant[:weight] += 10
=> 10

Picking sensible defaults may not always be easy.

Code along

Let's use the different forms of Hash::new to create some hashes in bin/code_along.rb.

Lab

In bin/lab.rb use Hash.new to create a Ruby Hash that has [] as the default for the keys :experience and :education. These defaults should be assigned to the appropriate key on access or assignment.

Assigning and accessing elements in a Ruby Hash

Demonstration

> consultant[:weight]
=> 0
> consultant[:weight] = 185
=> 185
> consultant[:weight] += 5
=> 190
> consultant[:surname]
=> "Donovan"
> consultant[:surname].prepend 'O\''
=> "O'Donovan"
> consultant[:surname]
=> "Donovan"
> consultant[:surname] = consultant[:surname].prepend 'O\''
=> "O'Donovan"
> consultant[:surname]
=> "O'Donovan"

To get an Array of the keys that have been set in a hash, use Hash#keys.

> consultant.keys
=> [:given_name, :surname, :height, :weight]

Code along

A quick look at access and assignment in bin/code_along.rb.

Lab

Append "GA WDI" to both :experience and :education in the hash in bin/lab.rb. Append one or more item of your choosing to the values at each of these keys.

Source code distributed under the MIT license. Text and other assets copyright General Assembly, Inc., all rights reserved.

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.