Ruby Built-In Data Types

Hello and welcome back to Project Immerse LLC – your #1 leading source for epic knowledge. That sounded epic for epic’s sake by the way. Alright, let’s get right to it.

I was thinking of creating a completely separate post for this, but I figured this would be a monumental moment to celebrate my transition into “Ruby Land”. Now, I’m not leaving “PHP Land” nor will I ever, it’s just I’ve spent a lot of time there. PHP is an amazing language, it’s been through some rough stuff over the years but PHP 7 shows us just how resilient PHP really is. I also have a handful of pet projects written in PHP so activity won’t cease there.

I was first introduced to Ruby by a junior developer, an intern, a mathematician. Can’t mention this persons’s name, but if you’re reading this – thank you for handing me that Ruby book. I wish I hadn’t tossed it, but I did because I knew I’d have it all in memory. I think you would appreciate that nerdy pun.

Ruby I think, is exactly how people describe it to be – elegant, simple, and productive, okay I can see that. I’ve read rumors that Ruby had some huge security holes, but so does the other languages. Write your code well and that can be solved.

Before I start getting into building small little programs, I like spending time with Data Types. Data Types are like the atoms of the programming world, specifically in software. Virtual particles created from energy, that though trips me out sometimes, actually quite frequently.

There are Numbers, Strings, True/False/Nil, Symbols, Arrays and Hashes. Numbers, Strings and Boolean (True/False/Nil) and Arrays are found in almost every language especially when compared to PHP and Javascript. So what I really want to focus on are Hashes and Symbols.

Hashes

Hashes are like associative arrays in PHP and Javascript, but Ruby decided to create an additional data type for it. Hashes behave a bit like a dictionary where each definition is connected to the word in question.
We can define a dictionary using key/value pairs, like below:


dictionary = { "captain_america" => "sheild", "thor" => "hammer", "tony_stark" => "iron man" }

// Now we can fetch it like this 
dictionary['captain_america']

// Output
// shield

Note that keys cannot have spaces, I think this is one of its downfalls however, there is a way around that but it requires parsing the keys and replacing each underscore with a space. I’ll cover that on a future post, but I think by then I would have figured out another optimal solution.

Symbols

Symbols behave differently when compared to Strings because String data types are stored in separate memory locations, but Symbols are stored by reference.

Check this out below:


"captain america".object_id

// Output 
=> 70168537783500

"captain america".object_id
=> 70168537685520

Same string, but different memory locations. But let’s try using Symbols this time:


:captain_america.object_id

// Output 
=> 1152028

:captain_america.object_id
=> 1152028

Same string, but SAME memory location. The property “object_id” is way to keep track and identify any object by a unique ID, which I think is pretty cool.

I think that’s a pretty good start. Now that we got a good handle on Data Types, we can spend our time pondering about the universe before we move on to Objects, Classes & Methods. Stay tuned and try immersing yourself in something new and different. Immerse on!

Leave a Reply

Your email address will not be published. Required fields are marked *