Gettings Started with Object Types: Variables, Integers, Floats, Strings & Arrays

Like I mentioned earlier everything in Ruby is an object _except_ one thing; a variable. Variables let us reference objects and as soon as they are referencing an object act just like that object. So they have one of two states; undefined or acting like an object. In ruby common practice to keep your variable names readable. They should all be in lowercase and connected with underscores (ie. new_variable).

The way Ruby reads numbers is either Integers or Floats. Integers are literally just whole numbers. One thing to note is that you can use assignment operators like += when using numbers to first carry out the operation then reassign the variable. For example if x = 5 and we said x += 3 … we would get an output of 8 and x would also be set to 8. One other thing to mention is Fignum and Bignum.. you will see come across these in Ruby and they are just subclasses of Integer. They just specify whether Ruby should be using a lot of memory to store the number (Bignum) or not (Fignum).

By definition a String object holds and manipulates a series of bytes that are usually characters. Most commonly they look like this => “Hello” .. ‘Hello’ is the same thing. Double quotes allow you to use escape or special characters inside strings. So for example if I were to say ‘I’m new’ you could see how that would be a problem, rather than “I’m new”. The best way view the single quotation as a literal is using a backslash in front of the character to tell ruby that it is a literal.So you would say “I\’m new”.

Lastly I’ll briefly mention arrays. An array is an ordered integer-indexed collection of objects. Which means you can specify what place objects are within an array and use that positioning to call all kinds of methods and manipulate the array however you want. For example [“a”, “b”, “c”, “d”, “e”] is an array. It’s important to note that in this case a is in the 0 position and b would be in the first, it’s zero indexed ordering that’s just how it is so keep that in mind as you go on. I’m going to keep things really simple and go step by step through everything so I’ll end this here. There’s obviously a lot more to know about these object types but I’m just trying to lay it out in terms anyone would understand.

Leave a Reply

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

You are commenting using your 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