Essentials for getting started with Ruby
if my_variable_is_true && my_other_variable_is_true
dosomething
elseif !my_next_variable_is_false
dosomethingelse
else
dothis
end
unless my_variable_is_true
# dosomething
end
|
object_id
|
The object ID
|
|
Boolean
|
true or false
|
|
numbers
|
|
|
strings
|
' or " quoted
|
|
odd
|
|
|
even
|
|
*
|
Repeat string
|
|
length
|
Number of characters in the string
|
|
reverse
|
A string with characters listed in reverse
|
|
to_sym
|
Convert to symbol
|
|
+
|
2+2
|
|
-
|
5-3
|
|
*
|
5*3
|
|
/
|
8/2
|
|
next
|
The next number
|
|
pred
|
The predecessor
|
|
&&
|
and
|
|
||
|
or
|
|
!
|
not
|
|
<
|
less than
|
|
>
|
greater than
|
|
<=
|
less than or equal to
|
|
>=
|
greater than or equal to
|
|
!=
|
not equal to
|
|
to_s
|
To string
|
|
new
|
Create an array
|
|
<<
|
Append to the end of an array
|
|
empty?
|
Is array empty
|
|
shift
|
Remove front element from array
|
|
unshift
|
Add new element at front of array
|
|
pop
|
Remove element from end of array
|
|
push
|
Add element to end of array
|
for my_placeholder in my_array
next if something_is_true
do_something
end
my_array.each do |my_placeholder|
next if something_is_true
do_something
end
my_array.each { |my_placeholder| next if something_is_true do_something }
|
has_key?
|
Has the key
|
|
has_value?
|
Has the value
|
|
my_hash[:my_key] = my_value
|
Set has value
|
def my_method(argument_1,
argument_with_default = 1,
*splat_argument)
# method body
# use return keyword to return a value
# better practice to omit return keyword
puts "argument_1: #{argument_1}"
puts "argument_with_default: #{argument_with_default}"
return splat.length
end
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.