Skip to content

Instantly share code, notes, and snippets.

View beezeebly's full-sized avatar

Beez beezeebly

View GitHub Profile
@beezeebly
beezeebly / gist:8152191
Created December 27, 2013 20:27
Ruby until example
i = 1
until i == 11
puts i
i += 1
end
@beezeebly
beezeebly / gist:8152352
Created December 27, 2013 20:39
Ruby single line unless
puts "This product is available" unless @private
@beezeebly
beezeebly / gist:8152829
Created December 27, 2013 21:26
Ruby each example
authors = ["Arthur Conan Doyle", "J.K. Rowling", "J.R.R. Tolkien", "Hanif Kureishi"]
authors.each do |author|
puts author
end
@beezeebly
beezeebly / gist:8153354
Created December 27, 2013 22:12
Ruby arithmetic operators
# Addition +
1 + 2 # result 3
# Subtraction -
2 - 1 # result 1
# Multiplication *
2 * 3 # result 6
# Division /
6 / 2 # result 3
# Modulus % returns the remainder of the left operand divided by the right operand
7 % 2 # result 1
@beezeebly
beezeebly / gist:8153441
Created December 27, 2013 22:19
Ruby comparison operators
a = 2
b = 3
# Equality ==
a == b # result false
# Not equal !=
a != b # result true
# Greater than >
a > b # result false
# Less than <
@beezeebly
beezeebly / gist:8153510
Created December 27, 2013 22:27
Ruby assignment operators
# equals =
a = 2
# +=
a += 1 # a is now 3
# -=
a -= 1 # a is now 2 again
# Ruby does not have ++ or --
# *=
a *= 2 # a is now 4
# /=
@beezeebly
beezeebly / gist:8153584
Created December 27, 2013 22:33
Ruby logical operators
# && the AND operator
true && true # result true
true && false # result false
false && false # result false
# || the OR operator
true || true # result true
true || false # result true
false || false # result false
# ! the NOT operator
!true # result false
@beezeebly
beezeebly / gist:8153667
Last active January 1, 2016 13:49
Ruby ternary operator
@in_stock = true
book_availability = @in_stock ? "In Stock" : "Awaiting Delivery"
puts book_availability # result "In Stock"
@beezeebly
beezeebly / gist:8173838
Created December 29, 2013 19:27
Ruby literal constructed array
authors = ["Arthur Conan Doyle", "J.K. Rowling", "J.R.R. Tolkien", "Hanif Kureishi"]
@beezeebly
beezeebly / gist:8173910
Created December 29, 2013 19:34
Ruby array element retrieval
authors = ["Arthur Conan Doyle", "J.K. Rowling", "J.R.R. Tolkien", "Hanif Kureishi"]
authors[0] # => "Arthur Conan Doyle"
authors[1] # => "J.K. Rowling"
authors[2] # => "J.R.R. Tolkien"
authors[3] # => "Hanif Kureishi"