Skip to content

Instantly share code, notes, and snippets.

@ytaras
Last active December 22, 2015 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ytaras/6497567 to your computer and use it in GitHub Desktop.
Save ytaras/6497567 to your computer and use it in GitHub Desktop.
# Dynamic typing
if false
"".foo
end
# Strong typing
[] + {}
{} + []
{} + ""
[] + ""
# Everything is an object
1.to_s
1.nil?
nil.nil?
# Open classes
class String
def foo
"foo"
end
end
"".foo
* Agenda
- [ ] Overview
- [ ] Check Ruby version and run IRb
- [ ] How to use IRb
- [ ] Dynamic vs static typing
- [ ] Strong vs weak typing
- [ ] Typing Game
- [ ] Object model
- [ ] Everything is an object
- [ ] Number
- [ ] Nil
- [ ] True/False
- [ ] Bit of syntax
- [ ] Class definition, instance creation
- [ ] Constructors, methods, attributes+properties
- [ ] attr_reader, attr_accessor, attr_writer
- [ ] Modules as mixins
- [ ] "Static" methods
- [ ] Duck typing
- [ ] Open classes
- [ ] Add method to String
- [ ] Add singleton method to object
- [ ] Cool stuff
- [ ] Blocks - &block syntax
- [ ] Blocks with params
- [ ] Optional params
- [ ] Hash syntax
- [ ] Block DSL pattern
- [ ] HtmlDsl
- [ ] Example dsl from inet
- [ ] Rails sample application
- [ ] rails new blog
- [ ] git init
- [ ] rvm use --ruby-version
- [ ] Add libraries
- [ ] simple_form
- [ ] compass
- [ ] zurb-foundation
- [ ] rails g foundation install
- [ ] rails g simple_form:install --foundation
- [ ] rails g scaffold Post title:string text:body
- [ ] Migration - run and explain
- [ ] Model - play in console
- [ ] Controller - read
- [ ] Routes, talk about REST
- [ ] Change app layout to use zurb grid
- [ ] Add flash
- [ ] Add root route
- [ ] Post editors
- [ ] Validation
- [ ] Simple format
- [ ] Prettier list of posts - time_ago_in_words
- [ ] Authentication
- [ ] Install Devise
- [ ] rails g devise:install
- [ ] rails g devise User
- [ ] Add login/logout links to application navigation bar
- [ ] rails g migration AddUserIdToPost user_id:integer
- [ ] Save creator id in post
- [ ] Show creator on index page
- [ ] N+1 select problem
- [ ] Comments
- [ ] acts_as_commentable
- [ ] rails g comment
- [ ] rails g controller Comments create
- [ ] ??? Count column???

Install Ruby

Linux or MacOS

\curl -L https://get.rvm.io | bash -s stable

See https://rvm.io/ for details

rvm install ruby-2.0
rvm use ruby-2.0
gem install rails
gem install bundler

Windows

  1. Best option Install VirtualBox with Linux image or Linux as double-boot
  2. Worse option Use Cygwin to install RVM as advised here - https://rvm.io/
  3. Worst option http://rubyinstaller.org/

Install IDE

Use whichever you like, we won't discuss that. If you don't have favorite, consider RubyMIne(IDE) or SublimeText2(editor); both will work just fine.

# Class definition
class Greeter
def hello(name)
puts "Hello, #{name}"
end
end
Greeter.new.hello('Yura')
# Constructor arguments
class Greeter
def initialize(name)
@name = name
end
def name
@name
end
def name=(name)
@name = name
end
def hello
puts "Hello, #{name}"
end
end
# Taste of metaprogramming
class Greeter
attr_reader :name
def initialize(name)
@name = name
end
end
# Mixins
module Fooable
def foo
puts "Foo"
end
end
class Fooator
include Fooable
end
# "Static" methods
class Singleton
def self.foo
puts "Foo"
end
end
def do_foo(fooator)
fooator.foo
end
# Meta-methods
Fooator.class
Fooator.new.class
Fooator.methods
Fooator.new.methods
Fooator.instance_methods
Fooator.instance_of? Class
Fooator.new.instance_of? Fooator
Fooator.new.instance_of? Fooable
Fooator.is_a? Fooatble
def method_with_block(&block)
puts "Before"
block.call()
puts "After"
end
method_with_block { puts "In" }
def block_with_params(arg, &block)
block.call(arg)
end
block_with_params('Hi there') { |a| puts a }
def optional_args(a = "")
puts "a = #{a}"
end
optional_args 1
optional_args
def hash_syntax(args)
puts args.inspect
end
hash_syntax cats: 2, dogs: 3
class HtmlDsl
def self.html(attributes = {}, &block)
dsl = HtmlDsl.new(attributes)
block.call dsl
dsl.build
end
attr_writer :title
def initialize(args)
@args = args
end
def build
self.inspect
end
end
HtmlDsl.html bgcolor: :red, fgcolor: :green do |h|
h.title = 'Hey there'
end
# Example DSL from inet - easy to build but few more concepts needed we're not going to cover
CustomHouse.build :home do
floor(1) {
room :den
room :kitchen
}
floor(2) {
room :bedroom
room :bathroom
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment