Skip to content

Instantly share code, notes, and snippets.

View beezeebly's full-sized avatar

Beez beezeebly

View GitHub Profile
@beezeebly
beezeebly / gist:8135766
Created December 26, 2013 16:33
Ruby programming style indent example.
class Book
attr_accessor :title, :isbn, :author, :price
def initialize(title, isbn, author, price)
@title = title
@isbn = isbn
@author = author
@price = price
end
end
@beezeebly
beezeebly / gist:8135845
Created December 26, 2013 16:42
Example of ruby code with mixed indentation
class Book
attr_accessor :title, :isbn, :author, :price
def initialize(title, isbn, author, price)
@title = title
@isbn = isbn
@author = author
@price = price
end
end
@beezeebly
beezeebly / gist:8137693
Created December 26, 2013 19:32
Example of a Ruby comment
class Book
# A comment
end
@beezeebly
beezeebly / gist:8138337
Created December 26, 2013 20:32
Naming conventions for Ruby
class FictionBook # camel case for class names
#...
end
def days_since_launch # snakes everywhere else
#...
end
STANDARD_PRICE = 10 # uppercase camel case for constants
@beezeebly
beezeebly / gist:8138670
Last active January 1, 2016 11:29
Example of Ruby parentheses conventions
# Parentheses are optional but usually preferred when defining or calling methods with parameters
def find_book(isbn)
# ...
end
find_book('978-0321584106')
# Parentheses are left off when methods have no parameters
@beezeebly
beezeebly / gist:8138922
Created December 26, 2013 21:27
Ruby single line statement
["John", "Mark", "Luke"].each { |name| puts name }
@beezeebly
beezeebly / gist:8138960
Created December 26, 2013 21:30
Ruby multi-statement block
["John", "Mark", "Luke"].each do |name|
puts "What's their name?"
puts "I think it's #{name}."
end
@beezeebly
beezeebly / gist:8151136
Created December 27, 2013 18:57
Ruby if/elsif/else control structure
if i < 0
puts "#{i} is a negative number"
elsif i > 0
puts "#{i} is a positive number"
else
puts "The number is zero"
end
@beezeebly
beezeebly / gist:8151195
Last active January 1, 2016 13:29
A Ruby unless control structure explanation
# Instead of if not
if not @private
puts "This product is available."
end
# use unless
unless @private
puts "This product is available."
@beezeebly
beezeebly / gist:8152142
Created December 27, 2013 20:24
Ruby while loop
i = 1
while i < 11
puts i
i += 1
end