Functions
def myNewFunction?
puts c
true
end
myNewFunction?
Arrays
animals = ['lions', 'tigers', 'bears']
animals.methods
animals.methods.include?('[]')
Hashes
numbers = {1 => 'one', 2 => 'two'}
stuff = {:array => [1, 2, 3], :string => 'Hi, mom!'}
stuff[:array]
Code Blocks & Yield
3.times {puts 'hiya there, kiddo'}
Code Blocks & Yield
animals = ['lions and ', 'tigers and', 'bears', 'oh my']
animals.each {|a| puts a}
Code Blocks & Yield
def call_block(&block)
block.call
end
def pass_block(&block)
call_block(&block)
end
pass_block {puts 'Hello, block'}
Classes
- Start with capitals, use CamelCase
- Instance Variables - @
- Class Variables - @@
- Methods/Vars - Lowercase with Underscores
- Test methods use a ?
Classes
class MyClass
attr_accessor :children, :node_name
def initialize(name, children=[])
@children = children
@node_name = name
end
def method1
end
end
Classes
class MyClass < otherClass
...
end
Mixins
Enumerable & Sets
- enumerable and comparable
- <=>
- sort, any?, all?, collect, select, max, member, inject
a = [5, 3, 4, 1]
a.inject(0) {|sum, i| sum + i}