12
Ruby Data Types Dalal Alrayes @dalrayes

Ruby data types

Embed Size (px)

Citation preview

Page 1: Ruby data types

Ruby Data TypesDalal Alrayes@dalrayes

Page 2: Ruby data types

Hi, I’m Dalal

Page 3: Ruby data types

Everything is an Object• An integer is an object 12

• A float is an object 1.333

• A string is an object “Ruby is awesome”

• An array is an object [“apple”,“orange”]

• A hash is an object { state: “Illinois”, zipcode: “60606”}

• & many, other types (blocks, lambdas, modules, classes) are also objects

Page 4: Ruby data types

Strings• Anything surrounded by

• Text based characters, typically representing words “Ruby is fun” “I am a string” “S”

• But can also be non-alphabet characters “78” “W2 2Sy”

quotes

Page 5: Ruby data types

String methods• Start up your terminal

• Type ‘irb’

• Let’s look up the docs

Page 6: Ruby data types

Arrays

bulls = [“Rose”, “Butler”, “Gasol”, “Noah”, “Brooks”]

0 1 2 3 4

• Each element in the array is associated with and referred to by an index

bulls[0] = ? bulls[4] = ?

• Ordered, integer-indexed collection• Can hold objects such as strings, integers, hashes and

other array objects

Page 7: Ruby data types

Indexing

array = [ “good”, “times”, 2015, “09”, “yup” ]

array[0] [1] [2] [3] [4]

array[-1][-2][-3][-4][-5]

Page 8: Ruby data types

Array methods• Start up your terminal

• Type ‘irb’

• Let’s look up the docs (google: ruby docs array)

Page 9: Ruby data types

Hashes• Dictionary like collection of unique key-value pairs

groceries = { “apples” => 3, “kale” => 1, “chips” => 7

}

• Retrieve or create an entry by using its key groceries[“apples”] groceries[“ice cream”] = 2

Page 10: Ruby data types

Hashes, ctd• Implicit Form

groceries = {“apples” => 3, “kale” => 1}

• Symbolsgroceries = {apples: 3, kale: 1}

style = {color: “red”, size: 3}

Page 11: Ruby data types

Hash methods• Start up your terminal

• Type ‘irb’

• Let’s look up the docs (try google: ruby 2.2 docs hash)

Page 12: Ruby data types

• Everything is an object

• Strings are anything in between quotes

• Arrays are indexed and start at 0

• Hashes are unique key-value pairs

• Check the official ruby docs for methods for each!

Recap