Skip to content
This repository was archived by the owner on Jul 26, 2020. It is now read-only.

eliotsykes/ruby-data-structures

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

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


About

Ruby Data Structures

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published