Skip to content

Instantly share code, notes, and snippets.

@maker-leo
Created March 19, 2013 12:17
Show Gist options
  • Save maker-leo/5195630 to your computer and use it in GitHub Desktop.
Save maker-leo/5195630 to your computer and use it in GitHub Desktop.
All the examples from our introduction to Ruby
def vader(evil = false)
# guard clause
return "something else" if evil == "luke"
# implicit return, last line executed
if evil
"i am your father and I hate you"
else
"i am your father and i love you"
end
end
puts "Luke, #{vader(true)}"
puts vader
puts vader("luke")
# eveything is an object
puts"string".class
puts 12345.class
puts 1.423423.class
puts false.class
puts true.class
puts nil.class
# what is the difference between false and nil
def empty_method
# this will just return nil
end
def false_method
# this will return false
false
end
if false
puts "this will never print"
end
if nil
puts "this will never print"
end
if "will return true"
puts "true"
end
if 0
puts "this will return true"
end
true_value = "string"
if true_value == true
puts "this wont print"
end
nil_value = nil
if nil_value.nil?
puts "this is nil"
end
# everything will pass an if condition
# except for false and nil
# this means "check this isn't false or nil"
if true_value
puts "this will print"
end
def something_true
true
end
def something_false
false
end
# and or should always be like this
if something_true && something_false
puts "wont print"
end
if something_false || something_true
puts "will print"
end
# strings
# these are two seperate objects
# we can change them and do what we like to them
string = "London"
another_string = "London"
# symbol
# immutable
# these are the same
# use a symbol as a placeholder
# :london
# :london
hash = { :london => "rainy", "London" => "still rainy"}
hash[:london]
# Bad idea if we change the string the hash gets messed up
hash["London"]
# belongs_to :users
# hashes and arrays
arr = [2,3,4,5,5,6,4]
arr << 10
puts arr.inspect
puts arr[0]
puts arr[0..2].inspect
# concatenation
arr = [1,2]
arr = arr + [3,4]
arr += [5,6]
puts arr.inspect
string += " is very rainy"
puts string
# loops
#
# don't do this way
for val in arr
puts val
end
# do it this way
# multi line block
arr.each do |v|
puts v
end
# one line block
arr.each { |v| puts v }
# there's other loops but we don't often use them
num = 0
while num < 5
puts "not infinity"
num += 1
end
num = -0
while true
puts "looping"
num +=1
break if num > 5
end
until num > 5
puts "nor infinity"
num += 1
end
# break stops the looping
[1,2,3].each {|a| puts a; break if a == 2 }
# next skips to the next element
[1,2,3].each {|a| next if a == 2; puts a }
5.times do |i|
puts i
end
# coding conventions
#
# spaces rather than tabs
# two spaces indentantion (but use the tab key)
# variable and method names should be snake_cased and lowercase
vader(false)
# leave off parentheses if you don't call an argument
vader()
# this is bad!!
vader (false)
# generally use parentheses when passing arguments to methods
vader false
vader(false)
puts("hello world")
puts "hello world"
# don't do this
def method no_parentheses
end
# do this
def method(parentheses)
end
# methods that return true/false end in a question mark
1.odd?
# methods that change the object you're calling end in a bang !
string = "london"
# this just returns a capitalized string
capital_string = string.capitalize
puts string
# wheraes this modifies the string we call the method on
string.capitalize!
puts string
puts capital_string
def pope_a_catholic?
true
end
puts pope_a_catholic?
def change_something!
# change our object
end
# try making an array with 10 elements and only printing the odd elements
# see how many ways you can do this
# hint there is a method called odd?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment