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