Upload
dhalrayes
View
169
Download
0
Embed Size (px)
Citation preview
Ruby Data TypesDalal Alrayes@dalrayes
Hi, I’m Dalal
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
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
String methods• Start up your terminal
• Type ‘irb’
• Let’s look up the docs
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
Indexing
array = [ “good”, “times”, 2015, “09”, “yup” ]
array[0] [1] [2] [3] [4]
array[-1][-2][-3][-4][-5]
Array methods• Start up your terminal
• Type ‘irb’
• Let’s look up the docs (google: ruby docs array)
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
Hashes, ctd• Implicit Form
groceries = {“apples” => 3, “kale” => 1}
• Symbolsgroceries = {apples: 3, kale: 1}
style = {color: “red”, size: 3}
Hash methods• Start up your terminal
• Type ‘irb’
• Let’s look up the docs (try google: ruby 2.2 docs hash)
• 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