Last active
August 29, 2015 14:01
-
-
Save greghaskins/96125653cfab7899914a to your computer and use it in GitHub Desktop.
Quick reference for getting started with Ruby. Useful for people doing katas, etc. with some programming knowledge but no Ruby experience.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These are some useful tips for programming in Ruby | |
# (Things in ALL_CAPS will be written by you) | |
# Comments begin with a "#" like this | |
# A test method | |
def test_SOMETHING | |
assert_equal EXPECTED_VALUE, ACTUAL_VALUE | |
end | |
# Functions | |
def FUNCTION_NAME(PARAMETER_NAME) | |
# ... | |
return RETURN_VALUE | |
end | |
# Example function for square of a number | |
def square(number) | |
return number * number | |
end | |
# square(4) => 16 | |
# square(5) => 25 | |
# square(7) => 49 | |
# If/else | |
if CONDITION | |
# do something when CONDITION is true | |
elsif OTHER_CONDITION | |
# otherwise do something when OTHER_CONDITION is true | |
elsif THIRD_CONDITION | |
# otherwise do something with THIRD_CONDITION is true | |
else | |
# do something else | |
end | |
# Math | |
x = 7 | |
y = 91 | |
y / x # => 13 | |
y % 13 # => 0 (modulo/remainder) | |
x % 3 # => 1 | |
z = y / x # => z is 13 | |
# Logic | |
x = 7 | |
x < 10 # => true | |
x < 5 # => false | |
x > 1 # => true | |
x > 19 # => false | |
x - 1 == 6 # => true | |
x % 5 == 0 # => false | |
# Strings | |
given_name = "Barak" | |
surname = "Obama" | |
full_name = given_name + surname # => "BarakObama" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment