Intro to Ruby Flashcards
What type of language is Ruby?
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
Data Types in Ruby
Arrays, Booleans, Hashes, Numbers, Strings, Symbols
Array
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
Is Ruby case sensitive?
Yes
Variable
Variables are ‘named’ storage locations for values.
We DECLARE variables and ASSIGN them values by using the assignment operator: =
Ruby Arithmetic Operators
Addition: + Subtraction: - Multiplication: * Division: / Exponentiation: ** Modulo: % (gives remainders of division)
‘print’ and ‘puts’ commands
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.
Classes, Objects and Methods
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)
Methods
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
Predicate Methods
methods that end with a ?.
they Result in (return) true or false, (return Boolean). Ex.:
5.odd?
=> true
5.between(11, 20)
=> false
Bang Methods
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
Methods and Variables
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
Structure of a Method
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
.length
return value is the number of characters in a string
https://ruby-doc.org/core-2.2.0/String.html
.reverse
return value is the backwards (reverse) version of the string object
.upcase
return value is the string is all UPPERCASE
.downcase
return value is the string is all lowercase
Single-Line Comments
use the # symbol #this is a single line comment
Multi-Line Comments
use =begin and =end =begin this is a multi-line comment =end
** =begin and =end need to be on their own lines
Variable Naming Convention
should start with lowercase letter
word should be separated by an underscore
e.g., my_first_bicycle_model
Chaining Methods
object.method1.method2.method3
Example
name = “Bill Jenkins”
puts name.downcase.reverse.upcase
return value => SNIKNEJ LLIB
.gets
Ruby methods that obtains information from the user
can be stored in a variable
How to print a variable inside of a string
Must be inside quotes
Must use # and { }
e.g. my_name = “Billy Jenkins”
print “My name is #{my_name}.”
.capitalize
capitalizes the first letter of the string
if / elsif / else
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”
unless
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
==
The double equal sign is always used to test whether to values are equal
A single equal sign (=) is used for assignment, not evaluation
!=
Not equal
Comparators (or Relational Operators)
==, !=, , <=, >=
Logical Operators (or Boolean Operators)
&& (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
.include? method
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)
.gsub! method
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