Skip to content

Instantly share code, notes, and snippets.

@csrobinson86
Last active August 29, 2015 13:58
Show Gist options
  • Save csrobinson86/9927112 to your computer and use it in GitHub Desktop.
Save csrobinson86/9927112 to your computer and use it in GitHub Desktop.
Ruby Syntax notes
# Primitive Data Types
Strings and numbers are known as primitive data types. Primitive data types are basically the smallest building blocks for Ruby programs.
Primitive data types define the characteristics of values.
#Boolean
Booleans are a primitive data type, just like Strings and Numbers. Booleans can have two values: true or false
# String interpolation
While "hello" + 3 doesn't make sense, you can mix strings and numbers if the return is meant to be an alpha-numeric string. Combining strings and numbers is called string interpolation. String interpolation is the act of inserting a non-string value into a String, thus resulting in a String. Interpolation is accomplished with this #{ } syntax
#Nil
Square brackets ([]) are used in Ruby to reference locations. When you typed "Bloc"[7] you instructed Ruby to return the 7th character in the "Bloc" string. As you know, this operation returned nil because there is no 7th character in the "Bloc" string.
Notice that "Bloc"[2] returned o. You instructed Ruby to return the 2nd character in the "Bloc" string, so why didn't it return l? The reason is that counting starts at 0 in programming languages. This means that "Bloc"[0] would return B.
#Symbol
here's one more primitive data type to explore before moving on: the Symbol. Symbols are like strings, only with less functionality. A Symbol is designated with a colon (:) to the left of a word. Symbols can contain any alpha-numeric character, but they must start with a letter. Type a few Symbols in the console:
p :hello
p :hi123
p :HELLO
The primary benefit of a symbol is that it is faster for a computer to process, as opposed to a string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment