Ruby Basics Flashcards
(36 cards)
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