Intro to Ruby Flashcards

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

What type of language is Ruby?

A

High-Level: looks more like english; closer to human language than computer language

Interpreted: An interpreter produces results while the program is executing, as opposed to a compiled language that is translated to machine code prior to running / Interpreted slower than Compiled

Object-Oriented: encapsulation, abstraction, polymorphism, inheritance

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

Data Types in Ruby

A

Arrays, Booleans, Hashes, Numbers, Strings, Symbols

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

Array

A

A collection of objects. Can be strings, numbers, other arrays, etc.

Created by enclosing objects in square brackets
[“x”, “y”, “z”]

Indexing starts with zero

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

Is Ruby case sensitive?

A

Yes

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

Variable

A

Variables are ‘named’ storage locations for values.

We DECLARE variables and ASSIGN them values by using the assignment operator: =

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

Ruby Arithmetic Operators

A
Addition: +
Subtraction: -
Multiplication: *
Division: /
Exponentiation: **
Modulo: %  (gives remainders of division)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

‘print’ and ‘puts’ commands

A

The ‘print’ command simply prints to the screen. Successive ‘print’ commands will print on same line.

The ‘puts’ command (short for put string) is similar but it will go to a new line after the value is printed.

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

Classes, Objects and Methods

A

Classes are high-level TYPES/CATEGORIES of things (e.g. Houses)

Objects are things that are specific INSTANCES of types (e.g. a specific House -> BJ House)

Methods act on objects. They’re an Expression that returns a value (e.g. Paint Blue the BJ House)

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

Methods

A

Methods implement the functionality of your program.

Methods are expressions or messages sent to the object that cause it to respond in a manner specific to the method instruction; instead of “calling a method”, Rubyists say “send a message” to the object

http: //ruby-for-beginners.rubymonstas.org/objects.html (best site)
http: //ruby-for-beginners.rubymonstas.org/writing_methods/definition.html

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

Predicate Methods

A

methods that end with a ?.

they Result in (return) true or false, (return Boolean). Ex.:

5.odd?
=> true

5.between(11, 20)
=> false

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

Bang Methods

A

methods that end with an !. They MODIFY the object they are called on. For example:

my_name = "Billy Jenkins"
puts my_name.upcase
    => BILLY JENKINS    (then..)
puts my_name
    => Billy Jenkins (same as original assignment)
However,
puts my_name.upcase!
    => BILLY JENKINS      (then..)
puts my_name
    => BILLY JENKINS
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Methods and Variables

A

Just as Variables are used to assign a name to an object, a Method is a way of assigning a name to a small executable piece of code

Variables name THINGS, Methods name BEHAVIOR

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

Structure of a Method

A

def gimme_bacon()
puts “Bacon plz.”
end

The def keyword is Ruby syntax that we want to DEFine a method
next, gimme_bacon is the Method NAME
next, the Method’s arguments in ( )
next, ‘puts “Bacon plz.” is the Method BODY
next, end marks the END of the Method

def add_two(number)
number + 2
end

https://ruby-doc.com/core/doc/syntax/methods_rdoc.html

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

.length

A

return value is the number of characters in a string

https://ruby-doc.org/core-2.2.0/String.html

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

.reverse

A

return value is the backwards (reverse) version of the string object

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

.upcase

A

return value is the string is all UPPERCASE

17
Q

.downcase

A

return value is the string is all lowercase

18
Q

Single-Line Comments

A
use the # symbol
#this is a single line comment
19
Q

Multi-Line Comments

A
use =begin and =end
=begin
this is a 
multi-line comment
=end

** =begin and =end need to be on their own lines

20
Q

Variable Naming Convention

A

should start with lowercase letter

word should be separated by an underscore

e.g., my_first_bicycle_model

21
Q

Chaining Methods

A

object.method1.method2.method3
Example
name = “Bill Jenkins”
puts name.downcase.reverse.upcase

return value => SNIKNEJ LLIB

22
Q

.gets

A

Ruby methods that obtains information from the user

can be stored in a variable

23
Q

How to print a variable inside of a string

A

Must be inside quotes
Must use # and { }
e.g. my_name = “Billy Jenkins”
print “My name is #{my_name}.”

24
Q

.capitalize

A

capitalizes the first letter of the string

25
Q

if / elsif / else

A

the if and elsif statement test whether an expression evaluates to true or false; if true, it executes the following line of code. if false, it moves on to the next elsif or else

  • must finish if/elsif statements with “end”
26
Q

unless

A

The unless statement is best for checking if something is false, rather than true. For example:

print “How are you feeling? “
user_state = gets.chomp

unless user_state == "Hungry"
  puts "I'm writing Ruby programs!"
else
  puts "Time to eat!"
end
27
Q

==

A

The double equal sign is always used to test whether to values are equal

A single equal sign (=) is used for assignment, not evaluation

28
Q

!=

A

Not equal

29
Q

Comparators (or Relational Operators)

A

==, !=, , <=, >=

30
Q

Logical Operators (or Boolean Operators)

A

&& (and), | | (or), and ! (not)

&& - both must be true to evaluate to true
| | - one must be true to evaluate to true
! - changes true to false and false to true

31
Q

.include? method

A

Returns true if String or String Variable contains the given string or character.

user_input = “Yes, please.”
if user_input.include? “s” (would evaluate to true)

32
Q

.gsub! method

A

Global SUBstitution method every instance of the first argument with the contents of the second argument

The syntax looks like this:
string_to_change.gsub!(/s/, “th”)

https://ruby-doc.org/core-2.2.0/String.html