Search for '{{search_term}}'

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

CMOS is the Code-Maven Open Source podcast that also includes video interviews. Subscribe to this feed RSS feed with your Podcast listener app or via iTunes iTunes.

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', 'Gree', 'Yellow' ]
puts colors.class # Array

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


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus