Skip to content

Instantly share code, notes, and snippets.

@dannyvassallo
Last active August 29, 2015 14:10
Show Gist options
  • Save dannyvassallo/d6c3e0199fb8077e6eda to your computer and use it in GitHub Desktop.
Save dannyvassallo/d6c3e0199fb8077e6eda to your computer and use it in GitHub Desktop.
BLOC Ruby Notes
=== STRING INTERPOLATION ===
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 new String. Interpolation is accomplished with this #{ } syntax.
EXAMPLES:
• p "hello #{3}"
• num = 5
• p "hello #{num}"
Notice the non-string values are contained inside of the #{}.
You can also add more to the String after the #{ } syntax.
The Ruby interpreter will run the code inside of the #{ }
first and then return it to the outer String. This will ultimately
return a String with all interpolated values accounted for.
EXAMPLES
• p "3 + 4 = #{3 + 4}"
• num1 = 3
• num2 = 4
• p "3 + 4 = #{num1 + num2}"
Interpolated strings must be enclosed in double quotes.
The Ruby interpreter will not search for interpolated
values in a string if single quotes are used:
BAD = p 'hello #{3}'
GOOD = p "hello #{3}"
=== DECIMALS / FLOATS ===
Decimals in programming are called Floats.
=== COMMAS / UNDERSCORES ===
Ruby allows an _ to be used in numbers for better readability.
Although an _ can be placed anywhere in a number, the best
practice is to place them where a comma would normally
be placed. (e.g. 1_000_000).
=== BRACKETS/NIL ===
p "Bloc"[7]
output nil
p "Bloc"[2]
output "o"
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.
You can assign a variable as nil.
EXAMPLE
nilly = nil
p nilly
output nil
=== METHOD/METHOD VOCAB ==
Call - run or execute a method.
Define - compose or write a method.
When we refer to the length of a method, we calculate
the lines between def and end. For example, the return_three
method below contains three lines of code.
EXAMPLE
def return_three
a = 1
b = 2
a + b
end
You can interact with methods by providing them with information.
Consider two more terms around methods:
Argument - information given to a method.
Pass - provide a method with information (an argument).
=== IMPLIED PARENTHESIS ===
Implied parentheses
Ruby syntax allows for "implied parentheses", meaning
that the Ruby interpreter will assume that method argument
means method(argument). Remember how we said p is really
just a Ruby method? That means that when you call p "Something",
you're really calling p("Something").
Implicit parentheses are one of the nicer features of Ruby,
as they make code easier to read and write. Be careful, though.
Sometimes the Ruby interpreter will add parentheses in different ways
than you intended. When in doubt, it's best to be explicit.
== Ranges ==
Ranges
Ruby supports Ranges, which are similar but not identical to arrays. Ranges allow us to specify a sequential set of strings or integers. A range looks like this:
(1..5)
("a".."e")
We can use a range to create an array, using the to_a ("to array") method:
(1..5).to_a
#=> [1, 2, 3, 4, 5]
("a".."e").to_a
#=> ["a", "b", "c", "d", "e"]
=== bang ===
The exclamation point is referred to as a "bang". In Ruby,
methods ending in a ! will usually change objects permanently
(or until the object is changed again).
Really, a bang signifies "danger". If your method modifies the
object it's called on, or raise an error if anything goes wrong,
end it in a ! to tell other developers to watch out. Note that
this behavior is unrelated to the usage of ! in if statements.
=== JOIN ===
[ "a", "b", "c" ].join
#=> "abc"
The join method can also take an argument, which it will
insert between each element of the array it's called on:
[ "a", "b", "c" ].join("-")
#=> "a-b-c"
join uses an empty string as a default argument. join is
the same as join('').
QUESTION
line-oriented string literals (Here document)
There's a line-oriente form of the string literals that is usually called as `here document'. Following a << you can specify a string or an identifier to terminate the string literal, and all lines following the current line up to the terminator are the value of the string. If the terminator is quoted, the type of quotes determines the type of the line-oriented string literal. Notice there must be no space between << and the terminator.
If the - placed before the delimiter, then all leading whitespcae characters (tabs or spaces) are stripped from input lines and the line containing delimiter. This allows here-documents within scripts to be indented in a natural fashion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment