Ruby Basics Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

abstraction

A

Abstraction ensures that as users, we’re far removed from what’s happening “under the hood”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

abstraction of ruby

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Domain Specific Languages (DSL’s)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

tab function

A

set to 2 spaces and indenting should be set to use spaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

sign

A

means anything after it, on the same line, is a comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

snake_case

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

constant

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

do/end blocks, prefer { }

A

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 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

CamelCase

A

naming your classes you will use CamelCase formatting. CamelCase uses no spaces and capitalizes every word.

# Class naming
  class MyFirstClass
  end
  class MySecondClass
  end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

API

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

mkdir

A

create a new directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

cd

A

change directory

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

touch

A

create a new file

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

rm

A

remove a file (delete)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

cd ..

A

To navigate out of the current folder to the one above

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

rmdir

A

remove a directory (delete)

17
Q

control-c

A

hold buttons to get out of an infinite loop

18
Q

RubyGems or “gems”

A

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

19
Q

pry

A

used for dubugging Ruby code. must add it to code.

Used similar to irb.

# preparation.rb
require "pry"

a = [1, 2, 3]
a &laquo_space;4
binding.pry # execution will pause here, allowing you to inspect all objects
puts a

20
Q

irb

A

a built in interactive environment. runs ruby code in the terminal.

21
Q

string

A

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!\''
22
Q

string interpolation

A

irb :001 > a = ‘ten’
=> “ten”

irb :002 > “My favorite number is #{a}!”
=> “My favorite number is ten!”

23
Q

symbols

A

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”

24
Q

Numbers

A

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
25
Q

nil

A

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

26
Q

modulo

A

% symbol

gives the remainder of a division operation

27
Q

== operator

A

This compares the object on the left of the == with the object on the right and returns either true or false.

28
Q

+ operator

A

String concatenation to join two strings together.

irb :001 > ‘foo’ + ‘foo’
=> “foofoo”

irb :002 > ‘foo’ + ‘bar’
=> “foobar”

29
Q

to_i

A

will convert a String to a Fixnum

irb :001 > ‘12’.to_i
=> 12

30
Q

to_f

A

will convert a String to a float

irb :005 > ‘4’.to_f
=> 4.0

31
Q

to_s

A

convert an integer or float to a string

32
Q

array

A

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]

33
Q

index

A

Each element in an array can be accessed via an index. The indexes are numbered starting at zero.

34
Q

hash

A

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’}

35
Q

expression

A

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.

36
Q

puts

A

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.