Skip to content

Instantly share code, notes, and snippets.

@jwoertink
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save jwoertink/8927593 to your computer and use it in GitHub Desktop.

Select an option

Save jwoertink/8927593 to your computer and use it in GitHub Desktop.
# Instance Variable
@people = [] # Array
# Local Variable
age = 32 # Integer
weight = 195.6 # Float
# = means assignment
person = {} # Hash
# String
word = "hello".upcase # method execution
# String interpolation
"#{word} there #{age}" # used with strings
# This is NOT string interpolation
# notice the single quotes. This is literal
'#{word} there #{age}'
def thing
# this is a method definition
end
# class
class Human # constant
end
# variables can change at any time
# constants can NOT change EVAR!!!
# < SomeConstant means inheritance
class Cat < Animal
# 2 types of methods
# Instance methods
# Class methods
# this is instance method
def walk
# instance methods only affect that instance
puts "walking"
end
end
# Object Instantiation
kitty = Cat.new #new is a class method. it's called on the class Cat
# kitty is an instance of Cat object
kitty.walk #walk is an instance method.
#it's called on the instance of the Cat class
# person is a hash
# hashes are key/value pairs
# adding a new key/vaule to an existing hash
person["name"] = "Jeremy"
# defining a new hash with default vaules
game = {"name" => "Mega Man"}
# @people is an array
# arrays are collections of objects
# arrays use a 0 based index
@people[0] = "Alex" # assign the value "Alex" to the index 0 in the array
@people << "Jeremy" # << is the put stuff into the array operator
@people.push("John") #push is an array method that does the same thing
# 1 is the 2nd index because 0,1,2,3,4,etc...
puts @people[1] # print out the name in the 2nd index of the array
# each is an iterator method that arrays have
@people.each do |name|
# name is a local variable inside of this code block
puts name
end #iterators that use the keyword "do" need an "end"
# inside the parenthesis is the arguments
# this method has 2 arguments
def encrypt(string, number)
# string is a local variable inside this method
# number is a local variable inside this method
end
# call the method, and pass 2 arguments
encrypt("hello", 10)
# the dot dot means it's a Range object
1..100 # a range of numbers 1 to 100
# convert the range to an array
(1..100).to_a
# multi-dimentional array
grid = [['a',1], ['b',2], ['c',3]]
cipher = Hash[grid] # convert multi-dimentional array to hash
cipher # {'a' => 1, 'b' => 2, 'c' => 3}
class Board
def initialize
# set initial values for the board object
# when you instantiate the class
# ruby runs this method when you call Board.new
end
end
#notice the File is using class methods, and file is using instance methods
# the ? means the method will return true / false
File.exists?('path/to/file.txt')
# open the file in read only mode
file = File.open('path/to/file.txt')
# http://apidock.com/ruby/IO for other modes
file.read #read 1 line of the file
file.close # close the file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment