ruby data types
TRANSCRIPT
![Page 1: Ruby data types](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/1.jpg)
Ruby Data TypesDalal Alrayes@dalrayes
![Page 2: Ruby data types](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/2.jpg)
Hi, I’m Dalal
![Page 3: Ruby data types](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/3.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/4.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/5.jpg)
String methods• Start up your terminal
• Type ‘irb’
• Let’s look up the docs
![Page 6: Ruby data types](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/6.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/7.jpg)
Indexing
array = [ “good”, “times”, 2015, “09”, “yup” ]
array[0] [1] [2] [3] [4]
array[-1][-2][-3][-4][-5]
![Page 8: Ruby data types](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/8.jpg)
Array methods• Start up your terminal
• Type ‘irb’
• Let’s look up the docs (google: ruby docs array)
![Page 9: Ruby data types](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/9.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/10.jpg)
Hashes, ctd• Implicit Form
groceries = {“apples” => 3, “kale” => 1}
• Symbolsgroceries = {apples: 3, kale: 1}
style = {color: “red”, size: 3}
![Page 11: Ruby data types](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/11.jpg)
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](https://reader031.vdocuments.net/reader031/viewer/2022030305/587179351a28ab230b8b5a2b/html5/thumbnails/12.jpg)
• 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