Created
March 6, 2014 09:06
-
-
Save bethesque/9385794 to your computer and use it in GitHub Desktop.
Naming conventions in Ruby
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
ClassNamesAreLikeThis | |
ModuleNamesAreLinkThis | |
def method_names_are_like_this | |
local_variable_names_are_like_this | |
@instance_variable_names_are_like_this | |
CONTSTANT_NAMES_ARE_LIKE_THIS | |
module Things | |
class UserThing | |
MAX_NAME_LENGTH = 100 # MAX_NAME_LENGTH is a constant, it doesn't change | |
def initialize name, address # name and address are arguments | |
@name = name # @name is an instance variable (it belongs to an instance of a class, see user variable below) | |
@address = address | |
end | |
# this is the code that creates the 'suburb' method | |
def suburb | |
parts = @address.split(",") # part is a local variable | |
parts.last | |
end | |
end | |
end | |
# This is how to make a new instance of UserThing - user is a local variable | |
user = Things::UserThing.new "Fred", "123 Short St, Melbourne") | |
puts user.suburb # This is how you use the 'suburb' method | |
puts Things::UserThing::MAX_NAME_LENGTH # This is how you use a constant |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment