Ruby Basics Flashcards
abstraction
Abstraction ensures that as users, we’re far removed from what’s happening “under the hood”
abstraction of ruby
Ruby is written in C, which translates to Assembly Language, which translates to machine language to translate 0s and 1s into something the computer understands.
Domain Specific Languages (DSL’s)
is a computer language specialized to a particular application domain. A higher level language based on a lower level. Rails or Rspec written in Ruby.
tab function
set to 2 spaces and indenting should be set to use spaces
sign
means anything after it, on the same line, is a comment
snake_case
When you define or initialize a method, variable, or file.
# Naming a file this_is_a_snake_cased_file.rb
# Assigning a variable forty_two = 42
# Defining a method def this_is_a_great_method # do stuff end
constant
When you want to represent a value that will not change in your Ruby program, you define a constant variable, often referred to as a constant. In Ruby, constants are denoted with all uppercase letters.
# Constant declaration FOUR = 'four' FIVE = 5
do/end blocks, prefer { }
when the entire code expression fits on one line.
# Multi-line [1, 2, 3].each do |i| # do stuff end
# Does the same thing in single line [1, 2, 3].each { |i| # do stuff }
CamelCase
naming your classes you will use CamelCase formatting. CamelCase uses no spaces and capitalizes every word.
# Class naming class MyFirstClass end
class MySecondClass end
API
API is an acronym for application programming interface, and is a somewhat overloaded term that can refer to both how applications talk to each other, as well as documentation. Just remember, if someone says “Did you take a look at the Array API?”, they’re talking about the Ruby docs documentation for the Array class. But if someone says “What’s the Twitter API?”, they’re talking about the programmatic interface to Twitter’s services.
mkdir
create a new directory
cd
change directory
touch
create a new file
rm
remove a file (delete)
cd ..
To navigate out of the current folder to the one above
rmdir
remove a directory (delete)
control-c
hold buttons to get out of an infinite loop
RubyGems or “gems”
There are two main sides to this term. The first side refers to a collection of Ruby files, or Ruby library, that performs a certain task. The other side refers to the publishing system that is behind organizing, listing, and publishing those libraries, or gems.
gem install
pry
used for dubugging Ruby code. must add it to code.
Used similar to irb.
# preparation.rb require "pry"
a = [1, 2, 3]
a «_space;4
binding.pry # execution will pause here, allowing you to inspect all objects
puts a
irb
a built in interactive environment. runs ruby code in the terminal.
string
A string is a list of characters in a specific sequence. Strings are surrounded by either single quotes (‘hi there’) or double quotes (“hi there”).
# Ex. 1: with double quotes "The man said, 'Hi there!'"
# Ex 2: with single quotes and escaping 'The man said, \'Hi there!\''
string interpolation
irb :001 > a = ‘ten’
=> “ten”
irb :002 > “My favorite number is #{a}!”
=> “My favorite number is ten!”
symbols
created by placing a colon (:) before a word
a symbol is used when you want to reference something like a string but don’t ever intend to print it to the screen or change it.
Examples of symbols
:name
:a_symbol
:“surprisingly, this is also a symbol”
Numbers
The most basic form of a number is called an integer. It is represented by the whole number only, with no decimal point. A more complex form of a number is called a float. A float is a number that contains a decimal.
# Example of integers 1, 2, 3, 50, 10, 4345098098
# Example of floats 1.2345, 2345.4267, 98.2234
nil
In programming, we need a way to express “nothing”, and in Ruby, we do this through something called nil. A variable with a value of nil could be described as having ‘nothing’ or being ‘completely empty’, or even just simply ‘not any specific type’.
It is possible to check if something is a nil type by using .nil?
irb :001 > “Hello, World”.nil?
=> false
modulo
% symbol
gives the remainder of a division operation
== operator
This compares the object on the left of the == with the object on the right and returns either true or false.
+ operator
String concatenation to join two strings together.
irb :001 > ‘foo’ + ‘foo’
=> “foofoo”
irb :002 > ‘foo’ + ‘bar’
=> “foobar”
to_i
will convert a String to a Fixnum
irb :001 > ‘12’.to_i
=> 12
to_f
will convert a String to a float
irb :005 > ‘4’.to_f
=> 4.0
to_s
convert an integer or float to a string
array
An array is used to organize information into an ordered list. The list can be made up of strings, integers, floats, booleans, or any other data type. In Ruby, an array is denoted by square brackets [ ]. Inside the brackets you can create a list of elements separated by commas.
irb :001 > [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
index
Each element in an array can be accessed via an index. The indexes are numbered starting at zero.
hash
A hash, sometimes referred to as a dictionary, is a set of key-value pairs. It is represented with curly braces { }. A key-value pair is an association where a key is assigned a specific value. A hash consists of a key, usually represented by a symbol, that points to a value (denoted using a =>) of any type of data.
irb :001 > {:dog => ‘barks’}
=> {:dog => ‘barks’}
expression
An expression is anything that can be evaluated, and pretty much everything you write in Ruby is an expression. An expression in Ruby always returns something, even if that’s an error message or nil.
puts
we’re telling Ruby to print something to the screen. However, puts does not return what is printed to the screen.
Expressions do something, but they also return something. The value returned is not necessarily the action that was performed.
irb :001 > puts ‘stuff’
stuff
=> nil
So you can see that the word stuff was printed to the console and then a nil, which is Ruby’s way of saying ‘nothing’, was returned. It’s important to understand that distinction.