DEV Community

Val Philip Lorejo
Val Philip Lorejo

Posted on

TIL Blog Learn Ruby

-Ruby is case sensitive

-Variables, give it a name and set it equal to a value

-6 arithmetic operators (+,-,,/,*,%

-puts and print is different, puts adds a new blank after printing

-Everything in Ruby is an object

-Methods are like skills

-String Methods:
.length
.reverse
.upcase
.downcase
.capitalize
-Adding ! at the end of method modifies value contained in variable
-include? method
-gsub! method (global substitution)

-starting with # is how to make comments (single line)
-Multi line comments start with =begin and =end; one line to themselves

-similar syntax with f string in python but no f included and uses # before bracket (string interpolation)
-gets is the method that gets input from user
-chomp removes extra line/whitespace

-Control Flow can select different outcomes
if, elsif, else, end
unless statement, reverse of if, checks for false

-3 Logical/Boolean operators
And(&&), Or(||), and not(!)

-Loops
while
until
for
2 dots or 3 dots for range, 2 dots includes least and highest, 3 dots means go up to but dont include highest value

-Loop method
can interchange {} with do and end
break
next keyword- used to skip over steps

-Use of arrays to store multiple values in a variable

-.each operator, apply an expression to each element of an object, same with loop method can make use of {} or do and end.
Iterates over hashes and arrays

-.times operator a super compact for loop

-.split method

-Each element in the array has an index, starts at 0 goes up to arrays length-1

-hashes similar to python dictionaries, key-value pairs, values assigned using =>
Hash.new creates new hash, replaces use of bracket syntax
When using Hash.new, can add new key-value pairs through use of bracket notation ex[key] = value
Access value through key

-.sort_by returns array of arrays
-can covert things to a string by using .to_s

-Methods syntax
Defined using def
Use of end
Similar to functions in python
Use of * means that there can be more than one argument put in

-Return statement

-Blocks are like methods with no name

-How Blocks differ from methods
Methods can be called many times as needed, but blocks only called once given the context

-Combined comparison operator
<=> returns 0, 1, or -1

-Symbols are different from strings, makes use of : before string. Symbols carry only one copy, unlike strings
-Immutable
-Saves memory due to having one copy at a time

-.intern and .to_sym same thing

-Ruby 1.9 syntax - no need for hash rocket anymore, just replace colon at beginning to end, similar to javascript and python

-.select selects certain values in hash

-Case statement, when using multiple conditions, this can be used besides if else, starts with case and variable name, nested when it is when statements that then end with else
Ex) when variable
when condition
..
else
end

-CRUD
create read update delete
Actions taken when updating a database

Top comments (0)