- string
.chars.count
: block receives each char and method will return the number of truthy results.split
.each_char
: block called for each char with the current char as the argument.with_index
: when used with.each_char
, will add a second argument, index, passed to the block
- number
.even?
/.odd?
.abs
.floor
,.ceil
,.round
.lcm
(8.lcm(12) => 24
).gcd
(8.gcd(12) => 4
).to_i
,.to_f
,.to_s
- boolean
- nil
- symbols
:+
:-
:*
:/
puts
: stringifies values and prints them to console
- Message
- Stack trace (ex:
(repl):1:in '<main>'
)
var = evaluated_expression
Use snake_case!
# Comment
or
=begin
Look!
I'm a comment
=end
+
, -
, *
, /
, **
, %
Use PE[MD][AS]
2 + 2.0 => 4.0
but 2 + 2 => 4
reciever.method(argument) => return value
def method_name(arg1, arg2)
arg1 + arg2 # implicit return
end
def has_unreachable(value)
return value
value = value + 1 # unreachable code
end
- heterogeneous arrays: different data types contained in the array
arr = [ 1, 2, 3, 4, 5]
0 1 2 3 4
-5 -4 -3 -2 -1
0..2 # inclusive 0, 1, 2
0...2 # exclusive 0, 1
arr[0..2] => [1, 2, 3]
arr[3..-1] => [4, 5]
arr.[](0) => 1
arr[5] => nil
("a".."c").to_a => ["a", "b", "c"]
(0..5).to_a = [0, 1, 2, 3, 4, 5]
.first
,.last
.length
.push
,.pop
,.shift
,.unshift
.join
.sort
,.sort!
.reverse
,.reverse!
.include?
.each
.each_index
.all?
: check receiver values to see if all values pass a test.none?
: check receiver values to make sure none pass the test.any?
: check receiver values to see if any pass the test.map
,.map!
: create a new array based on the return value of the passed block.count
: returns the length of the array, or the number of matching values, or the number of elements that pass a block test.select
,.select!
: return new array with all elements that passed the block test.reject
,.reject!
: return new array with all elements that did NOT pass the block test.sort_by
: block should return a number that represents how each element should be sorted (least to greatest).each_with_index
: block has to arguments, value and index, for each element.with_index
: can be used to add an index argument to the block when used with.map
.reduce
,.inject
: when used with a symbol (ex::+
) the elements will be combined using that symbol (so all elements would be added together). When used with a block, the block will receive an accumulator and element argument. The first accumulator can be passed in as a method parameter; otherwise, it uses the first value in the receiver.
arr[3..-1] = 6, 7 => [1, 2, 3, 6, 7]
arr[7] = 8 => [1, 2, 3, 4, 5, nil, nil, 8]
arr.concat([6, 7]) => [1, 2, 3, 4, 5, 6, 7]
arr = arr + [6, 7] => [1, 2, 3, 4, 5, 6, 7]
arr << [6, 7] => [1, 2, 3, 4, 5, [6, 7]]
["cat"] == ["dog"] => false
["cat"] >= ["dog"] => throws error
aa = "App Academy"
aa[3] => " "
aa[0..2] => "App"
aa[0..3] = "Best " => "BestAcademy"
"echo" * 3 => "echoechoecho"
aa << "!" => "App Academy!"
aa.concat("!")
.chars
.length
.reverse
,.reverse!
.include?
.upcase
,.downcase
,.capitalize
.split