Basic data structures in Ruby (Scalar, Array, Hash)

In Ruby there are 3 basic data structures.

Scalars can hold a single value: a number or string.

Arrays is an ordered list of scalars.

Hashes are key-value pairs where the keys are uniques strings and the values are scalars

The class method can tell us what kind of value a variable contains:


x = 43
puts x.class      # Fixnum

q = 3.14
puts q.class      # Float

z = "abc"
puts z.class      # String

colors = [ 'Blue', 'Green', 'Yellow' ]
puts colors.class # Array

person = { 'fname' => 'Foo', 'lname' => 'Bar' }
puts person.class  # Hash


timestamp: 2015-10-12T12:01:01 tags:

  • class
  • Fixnum
  • Float
  • String
  • Array
  • Hash