Introduction to Ruby, Rails, and Ruby on Rails
-
Upload
simon-bagreev -
Category
Technology
-
view
385 -
download
7
description
Transcript of Introduction to Ruby, Rails, and Ruby on Rails
Simon BagreevTwitter: @status_200Github: semminEmail: [email protected]
Simon BagreevTwitter: @status_200Github: semminEmail: [email protected]
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
Knowing Rails != Knowing Ruby
Knowing Rails != Knowing Ruby
simple and efficient
duck-typed
functional
metaprogramming
simple and efficient
duck-typed
functional
metaprogramming
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
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
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
x = x + 1x = x + 1x - x = 1x - x = 1
0 = 10 = 1
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"]
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!
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
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
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=...
Framework does things for youFramework does things for you
== awesome== awesome
Don’t Repeat YourselfDon’t Repeat YourselfConvention Over ConfigurationConvention Over Configuration
Homebrew, XCodeHomebrew, XCodeDatabaseDatabaseRuby Version Manager (RVM)Ruby Version Manager (RVM)Ruby, RailsRuby, RailsBundlerBundlerGitGitHeroku ToolbeltHeroku Toolbelt
You are ready to build your first You are ready to build your first Rails application!Rails application!
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
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
Thank You!Thank You!