Introduction to Ruby, Rails, and Ruby on Rails

21
Simon Bagreev Twitter: @status_200 Github: semmin Email: [email protected]

description

Given at Dominion Enterprises software developers users group in Dec, 2011. Purpose -- introduce Ruby and Rails to the group. Original presentation consisted of two parts: slides and live Rails application building. Slides are included here.

Transcript of Introduction to Ruby, Rails, and Ruby on Rails

Page 1: Introduction to Ruby, Rails, and Ruby on Rails

Simon BagreevTwitter: @status_200Github: semminEmail: [email protected]

Simon BagreevTwitter: @status_200Github: semminEmail: [email protected]

Page 2: Introduction to Ruby, Rails, and Ruby on Rails

Programs must be written for people to read, and Programs must be written for people to read, and only incidentally for machines to execute.only incidentally for machines to execute.

Abelson and SussmanAbelson and Sussman

... We are the masters. They are the slaves. ... We are the masters. They are the slaves. Yukihiro Matsumoto, creator of Ruby Yukihiro Matsumoto, creator of Ruby

on people and machineson people and machines

Page 3: Introduction to Ruby, Rails, and Ruby on Rails

Knowing Rails != Knowing Ruby

Knowing Rails != Knowing Ruby

Page 4: Introduction to Ruby, Rails, and Ruby on Rails

simple and efficient

duck-typed

functional

metaprogramming

simple and efficient

duck-typed

functional

metaprogramming

Page 5: Introduction to Ruby, Rails, and Ruby on Rails

module Fooinizer def fooinize() self.split(" ").map{|t| t + "foo"}.join(" ") endend

# String.send(:include, Fooinizer)class String include Fooinizerend

puts "my nifty string".fooinize# => myfoo niftyfoo stringfoo

Page 6: Introduction to Ruby, Rails, and Ruby on Rails

If you quack like a duck, you must be a duckIf you quack like a duck, you must be a duckdef print_size(item) puts "This item size is #{item.size}"end

# Stringitem = "This is string"print_size(item) # => This item size is 14

# Arrayitem = %w(This item is array)print_size(item) # => This item size is 4

#Fileitem = File::Stat.new("efficiency.rb")print_size(item) # => This item size is 229

# Integeritem = 5print_size(item) # => This item size is 8

Page 7: Introduction to Ruby, Rails, and Ruby on Rails

No mutable data (== no side effects)

No state (== no hidden state)

Once assigned, “variable” doesn‘t change its value

Pure mathematical functions, i.e. f(x) == f(x) always, no matter how many times you run it

No mutable data (== no side effects)

No state (== no hidden state)

Once assigned, “variable” doesn‘t change its value

Pure mathematical functions, i.e. f(x) == f(x) always, no matter how many times you run it

Page 8: Introduction to Ruby, Rails, and Ruby on Rails

x = x + 1x = x + 1x - x = 1x - x = 1

0 = 10 = 1

Page 9: Introduction to Ruby, Rails, and Ruby on Rails

Programmer has a choice between imperative and Programmer has a choice between imperative and functionalfunctional

# Imperativedays = %w(Mon Tue Wed)days << "Thu"days += ["Fri", "Sat", "Sun"]days # ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

# Functionaldays = %w(Mon Tue Wed)all_days = days + ["Thu"] + ["Fri", "Sat", "Sun"]all_days # ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

Page 10: Introduction to Ruby, Rails, and Ruby on Rails

Functions are first-class citizens (can be passed Functions are first-class citizens (can be passed and returned as other values)and returned as other values)

# Passing a functionhello_proc = proc { puts "hello world!"}

def some_method(&some_proc) some_proc.callend

some_method &hello_proc # => hello world!

# Returning a functiondef greeting_builder(*args) proc { puts "Hello #{args.join(" ")}!" }end

new_greeting = greeting_builder("Nifty", "Functional", "Ruby")new_greeting.call # => Hello Nifty Functional Ruby!

Page 11: Introduction to Ruby, Rails, and Ruby on Rails

Recall FoonizerRecall Foonizermodule Fooinizer def fooinize() self.split(" ").map{|t| t + "foo"}.join(" ") endend

# String.send(:include, Fooinizer)class String include Fooinizerend

puts "my nifty string".fooinize# => myfoo niftyfoo stringfoo

Higher order functions: mapHigher order functions: map

Page 12: Introduction to Ruby, Rails, and Ruby on Rails

Code that writes codeCode that writes code

# Classicclass Car def make @make end

def make=(value) @make=value endend

# Metaprogramming - method defines methodclass Car attr_accessor :make # defines getter and setterend

# Add methods to Car at runtimeCar.class_eval %{ attr_accessor :year }c = Car.new()puts c.respond_to? :year # => true

Page 13: Introduction to Ruby, Rails, and Ruby on Rails

Define a Class with methods from within another ClassDefine a Class with methods from within another Class

class Definer def self.build_custom_class(class_name, *methods) methods.each do |m| class_eval %{ class ::#{class_name}

attr_accessor :#{m} end } end endend

Definer.build_custom_class("Foo", "bar", "baz")f = Foo.newputs f.methods # => bar bar= baz baz=...

Page 14: Introduction to Ruby, Rails, and Ruby on Rails
Page 15: Introduction to Ruby, Rails, and Ruby on Rails

Framework does things for youFramework does things for you

== awesome== awesome

Page 16: Introduction to Ruby, Rails, and Ruby on Rails

Don’t Repeat YourselfDon’t Repeat YourselfConvention Over ConfigurationConvention Over Configuration

Page 17: Introduction to Ruby, Rails, and Ruby on Rails

Homebrew, XCodeHomebrew, XCodeDatabaseDatabaseRuby Version Manager (RVM)Ruby Version Manager (RVM)Ruby, RailsRuby, RailsBundlerBundlerGitGitHeroku ToolbeltHeroku Toolbelt

Page 18: Introduction to Ruby, Rails, and Ruby on Rails

You are ready to build your first You are ready to build your first Rails application!Rails application!

Page 19: Introduction to Ruby, Rails, and Ruby on Rails

ruby.railstutorial.org/ruby-on-rails-tutorial-ruby.railstutorial.org/ruby-on-rails-tutorial-bookbookedgeguides.rubyonrails.orgedgeguides.rubyonrails.orgtryruby.orgtryruby.orgrailsforzombies.orgrailsforzombies.orgrailscasts.comrailscasts.comgithub.com/rails/rails – rails source codegithub.com/rails/rails – rails source codegithub.com/semmin/intro-to-ror-preso – this github.com/semmin/intro-to-ror-preso – this presentationpresentation

Page 20: Introduction to Ruby, Rails, and Ruby on Rails

Arkin, McAnally, Ruby in Practice, Manning Publications, March 5, 2008Arkin, McAnally, Ruby in Practice, Manning Publications, March 5, 2008Khell’s blog, khelll.com/blog/ruby/ruby-and-functional-programmingKhell’s blog, khelll.com/blog/ruby/ruby-and-functional-programmingStuart, www.rubyinside.com/functional-programming-in-ruby-2713.htmlStuart, www.rubyinside.com/functional-programming-in-ruby-2713.html

Page 21: Introduction to Ruby, Rails, and Ruby on Rails

Thank You!Thank You!