Cheatography
https://cheatography.com
Essentials for getting started with Ruby
Command Line
From the command prompt run Ruby, providing the name of a file with source code:
$ ruby my_source-code.rb |
Interactive Ruby
The Interactive Ruby Shell can be used for interactive programming:
$ irb
>> |
Loading files
In IRB, files can be loaded using the load command:
>> load 'script_name.rb' |
Input
gets gets a string e.g.:
direction = gets.chomp |
Indentation
Useful but not necessary. |
If / Else Loops
if my_variable_is_true && my_other_variable_is_true
dosomething
elseif !my_next_variable_is_false
dosomethingelse
else
dothis
end
|
for loop - next
next skips to the next element |
Unless
unless my_variable_is_true
# dosomething
end
|
Console output
Use puts to display output. |
Data Types
Boolean |
true or false |
numbers |
strings |
' or " quoted |
|
|
String Methods
* |
Repeat string |
length |
Number of characters in the string |
reverse |
A string with characters listed in reverse |
to_sym |
Convert to symbol |
String Interpolation
Can be used to insert variable values in a double quoted string:
“String with interpolation: #{variable_name}” |
Operators
+ |
2+2 |
- |
5-3 |
* |
5*3 |
/ |
8/2 |
next |
The next number |
pred |
The predecessor |
Comparison operators
< |
less than |
> |
greater than |
<= |
less than or equal to |
>= |
greater than or equal to |
!= |
not equal to |
Ranges
.. from first to last
... from first to predecessor before last |
Symbols
Created using :
eg string_test = :test_symbol
More efficient than strings - only stored once |
|
|
Arrays
Described in [] with items delimited by commas:
meal = ['breakfast', 'lunch', 'dinner'] |
Array methods
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 |
Array loop
for my_placeholder in my_array
next if something_is_true
do_something
end
|
Array loop example 2
my_array.each do |my_placeholder|
next if something_is_true
do_something
end
|
Array loop example 3
my_array.each { |my_placeholder| next if something_is_true do_something }
|
Hashes
Contain key value pairs. Can use symbols for keys. |
Hash methods
has_key? |
Has the key |
has_value? |
Has the value |
my_hash[:my_key] = my_value |
Set has value |
References
Ruby Wizardry by Eric Weinstein |
Defining Methods
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
|
Yield in Blocks
The yield keyword is used to invoke a calling block from within a method. Control is passed back to the method after the block has been invoked. |
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by Simon Fermor