Types of Objects in Ruby Cont..

Last post I just briefly started talking about Arrays but there is a few cool things you can do with them that I didn’t mention. I can call methods on array to edit or find out a number of things about them. For example I can see what’s actually in the array using .inspect …. I can tie everything in the array together using .to_s or .join …. .join is special though because I can join the array with another object if I want. In this case I can say array.join(“, “)  and it will join all the objects in my array with a comma then a space. Some other things I can do is add things to the end of my array using the >> (append) function or .push will do the same thing. The opposite of .push is .pop where I would be pulling the last object out. The important thing to know about arrays is that they are ordered. If the order matters to my list of objects then I will use an array, allowing me to filter and search by the position an object is in.

Alternatively if order doesn’t matter I would use a Hash. Rather than being integer indexed (can search by number) a hash is indexed by object, meaning if you are looking for something inside your hash you couldn’t specify by which place it was in you would have to search by the object name. To keep track of the positions within a hash we use a key, value pair. A key is an object that holds the value which is another object.(ie.  first_name => “John” … first_name is the key and “John” is the value).. In almost all cases we will be looking for something inside a hash based on the key but if for some reason we wanted to find the key based on the value we would use the .index method to do that. One thing that might come in handy that is worth mentioning is to_a (means: to an array) which puts all the key value pairs into their own arrays inside of a bigger array that is your entire hash.

The next object I want to cover is Symbols. Symbols are just a label that is used to identify a piece of data. They are always preceded by a colon, so for example :test is a symbol and so is :this_test. They are good to use when all you’re looking for is a label but to clarify they are not variables. Lastly, they are immutable objects or they don’t change.

A Boolean represents either a true or a false value, it can’t be anything else. A few of the symbols that are common to see with booleans are == which means equal to, it’s evaluating whether something is equal to something else not setting the value. || two pipes is the “or” symbol. You could say 1 <= 5 || 10 >= 50  which would return true. The exclamation mark means not. 10 != 9 .. here 10 obviously does not equal nine. Lastly && is simply and. So if I said 1 >= 5 && 10 >= 50 it would return false.

Next up is a Range. This is really simple, instead of saying [1,2,3,4,5,6,7,8,9,10] in an array we can say 1..10 in a range. There’s a couple important things to know here: the difference between 1..10 and 1…10 is significant. Two periods is an inclusive range and three dots is exclusive. It’s recommended you just always use two so you don’t confuse yourself.

The last type of object is the Constant. Constants are not meant to change, they are constant (stay they same), similar to symbols. Anything that starts with a capital letter is a constant but typically they are in all capitals.

OK that’s it. A really brief overview of the object types in Ruby. Like anything there is way more to know about it but the basics will let us get to the next step from there we can look up methods we can call on an array or whatever we need to in the Ruby documentation.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s