Giter Club home page Giter Club logo

ruby-data-structures's Introduction

Ruby Data Structures


Arrays

Ruby Dynamic Arrays

Ruby Arrays are dynamic. Dynamic means that the size of the array can expand and contract. Contrast this to static arrays. Static arrays are used in some programming languages but not Ruby. Static arrays have fixed sizes and cannot expand to contain any more objects than the size the array was initially created with.

With dynamic arrays, and so with Ruby, we don’t have to worry about if an array is large enough to contain any new objects added to it.

Ruby Array and related API docs

For a good bedtime read, check out all of the methods and operators available to Ruby Arrays. Consider experimenting with any of these Array methods that you find interesting in irb.
http://www.ruby-doc.org/core-2.1.4/Array.html

Ruby Arrays are Enumerables. Enumerable is a module included in many of Ruby’s collection data structures, and provides many of the collection-related methods
http://www.ruby-doc.org/core-2.1.4/Enumerable.html

Multi-dimensional Arrays

Multi-dimensional arrays are arrays that contain arrays. This is an example of a 2 dimensional array:

[ [1, 7, 9, 11] , [223, 329, 2302], [33] ]

One example use of a 2-dimensional array is for holding sorted tabular data:

contacts = [
  ['Bruce Wayne', 'Batman', '555-333-444'],
  ['Clark Kent', 'Superman', '555-123-456'],
  ['Peter Parker', 'Spiderman', '555-111-222']
  ...
]

Using this 2-D array as a data structure for storing contact records would allow us to build an HTML table like this, where each row is a contact with 3 columns (name, superhero name, and phone number):

<table>
<% contacts.each do |contact| %>
  <tr>
    <% contact.each do |attribute| %>
      <td><%= attribute %></td>
    <% end %>
  </tr>
<% end %>
</table>

RubyMonk Arrays (Intro)

You can complete the exercises on the pages and navigate through the sections using the Next > link on the navbar stuck to the bottom of each page
http://rubymonk.com/learning/books/1-ruby-primer/chapters/1-arrays

RubyMonk Arrays (Advanced)

Some good exercises in this section, including an exercise that introduces other data structures (Stack & Queue)
http://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/33-advanced-arrays/

Reinventing Arrays in Ruby

This seemed like a fun way to finish up Arrays in Ruby - by seeing how you could write your own Array class without relying on Ruby’s provided Array
http://stackoverflow.com/questions/1571349/can-the-array-be-reinvented-in-ruby (first solution)


Contributors


ruby-data-structures's People

Contributors

eliotsykes avatar

Stargazers

Gaurav avatar Ben Barber avatar Jaya Wijono avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

acushlakoncept

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.