Skip to content

Instantly share code, notes, and snippets.

@malpinder
Last active August 29, 2015 13:57
Show Gist options
  • Save malpinder/9363727 to your computer and use it in GitHub Desktop.
Save malpinder/9363727 to your computer and use it in GitHub Desktop.
Cheat sheet for ruby syntax
# Comments start with a hash symbol.
# Casing rules:
variables_all_go_in_snake_case
method_names_in_snake_case_too
CONSTANTS_ARE_IN_SCREAMING_SNAKE_CASE
# Basic objects:
strings_have_single_quotes = 'A string'
strings_have_double_quotes_too = "A different string"
strings_can_be_interpolated = "I am interpolated with the integer #{ 12 }"
symbols_have_a_colon = :a_symbol
numbers_are_bare = 1
nil_is_bare_too = nil
and_true = true
and_false = false
arrays_use_square_brackets = [ 1,2,3 ]
hashes_use_curly_brackets = { :key => "value", :other_key => "other value" }
ranges_are_easier_with_round_brackets = (1..5)
# Defining a method
def method_name(any, arguments)
do_a_thing_here
with = any + arguments
end
# Calling a method
any_object.some_method(any, argmuents)
#Simple if statements
if some_conditional
do_this
end
# Complex if statements
if some_conditional
run_only_this_bit
elsif some_other_conditional
run_this_bit_instead
else
do_this
end
# Unless statements
unless some_conditional
do_this
end
# Inline if statements
do_a_thing if some_conditional
# Inline unless statements
do_a_thing unless some_conditional
# Ternary statements
some_conditional ? do_this : or_do_this
# Blocks (for the each method)
[1,2,3].each do |number|
do_a_thing_with(number)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment