Really compact Ruby quick reference guide
Hello World in Ruby
General Rules
Comments start with # and ends with newline
Newlines or semicolon can be used separate expressions
Everything in Ruby is an object. This includes constants such as 5
Since even operators are methods, they can be overridden
Elegant code can be written using blocks feature
Ruby Keywords
alias and BEGIN begin break case class def defined do else elsif END end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield
Ruby Types
Basic types are numbers, strings, ranges, regexen, symbols, arrays, and hashes.
Symbols
:symbol
Unique and comparable values that can be substituted for string keys.
Ranges
1..10 - includes last value (10)
1…10 - excludes last value (10)
Arrays
var = Array.new
var = [10,20]
var = %w (string1,string2)
Hashes
var = Hash.new
var = {1=>2, 2=>3, 3=>4}
Variable Types
$global_variables
@@class_variables
@instance_variables
CONSTANT (caps)
local_variables
Dummy Variables
self - current method’s object
nil
true,false
__FILE__
__LINE__
Predefined Variables
$! The exception message set by raise call. $@ Array of backtrace of the last exception thrown. $& The string matched by the last successful pattern match. $` The string to the left of the last successful match. $' The string to the right of the last successful match. $+ The last bracket matched by the last successful match. $1 The Nth group of the last successful match. May be > 1. $~ The information about the last match. $= The flag for case insensitive, nil by default. $/ The input record separator, newline by default. $ The output record separator for the print and IO#write. $, The output field separator for the print and Array#join. $; The default separator for String#split. $. The current input line number of the last file that was read. $< The virtual concatenation file of the files from command line. $> The default output for print, printf. $stdout by default. $_ The last input line of string by gets or readline. $0 Contains the name of the script being executed. $* Command line arguments given for the script sans args. $$ The process number of the Ruby running this script. $? The status of the last executed child process. $: Load path for scripts and binary modules by load or require. $" The array contains the module names loaded by require. $DEBUG The status of the -d switch. $FILENAME Current input file from $<. Same as $<.filename. $LOAD_PATH The alias to the $:. $stderr The current standard error output. $stdin The current standard input. $stdout The current standard output. $VERBOSE The verbose flag, which is set by the -v switch.
Operator Precedence (highest to lowest)
:: .
[]
**
-(unary) +(unary) ! ~
* / %
+ -
<< >>
&
| ^
> >= < <=
<=> == === != =~ !~
&&
||
.. …
=(+=, -=…)
not
and or
Ruby Control Structures
Method Invocation and Definition
method_call
object.method_call
Class::method_call
def method_name
method_body
end
Blocks
method_invocation do … end
method_invocation {
…
}
Exception Handling (2 forms)
Classes
Accessor Shortcuts
attr_reader
attr_writer
attr
[...] Pois eu e o blog Ruby Tips discordamos. Basta ler este “Really compact Ruby Quick Reference Guide“ [...]