Click here to load reader

Introduction to Ruby, Rails, and Ruby on Rails

  • View
    307

  • Download
    7

Embed Size (px)

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.

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

  • 1. Simon Bagreev Twitter: @status_200 Github: semmin Email: simon.bagreev@employmentguide.com

2. Programs must be written for people to read, and onlyPrograms must be written for people to read, and only incidentally for machines to execute.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 RubyYukihiro Matsumoto, creator of Ruby on people and machineson people and machines 3. Knowing Rails != Knowing Ruby 4. simple and efficient duck-typed functional metaprogramming 5. module Fooinizer def fooinize() self.split(" ").map{|t| t + "foo"}.join(" ") end end # String.send(:include, Fooinizer) class String include Fooinizer end puts "my nifty string".fooinize # => myfoo niftyfoo stringfoo 6. If you quack like a duck, you must be a duckIf you quack like a duck, you must be a duck def print_size(item) puts "This item size is #{item.size}" end # String item = "This is string" print_size(item) # => This item size is 14 # Array item = %w(This item is array) print_size(item) # => This item size is 4 #File item = File::Stat.new("efficiency.rb") print_size(item) # => This item size is 229 # Integer item = 5 print_size(item) # => This item size is 8 7. No mutable data (== no side effects) No state (== no hidden state) Once assigned, variable doesnt change its value Pure mathematical functions, i.e. f(x) == f(x) always, no matter how many times you run it 8. x = x + 1x = x + 1 x - x = 1x - x = 1 0 = 10 = 1 9. Programmer has a choice between imperative and functionalProgrammer has a choice between imperative and functional # Imperative days = %w(Mon Tue Wed) days hello world! # Returning a function def greeting_builder(*args) proc { puts "Hello #{args.join(" ")}!" } end new_greeting = greeting_builder("Nifty", "Functional", "Ruby") new_greeting.call # => Hello Nifty Functional Ruby! 11. Recall FoonizerRecall Foonizer module Fooinizer def fooinize() self.split(" ").map{|t| t + "foo"}.join(" ") end end # String.send(:include, Fooinizer) class String include Fooinizer end puts "my nifty string".fooinize # => myfoo niftyfoo stringfoo Higher order functions: mapHigher order functions: map 12. Code that writes codeCode that writes code # Classic class Car def make @make end def make=(value) @make=value end end # Metaprogramming - method defines method class Car attr_accessor :make # defines getter and setter end # Add methods to Car at runtime Car.class_eval %{ attr_accessor :year } c = Car.new() puts c.respond_to? :year # => true 13. 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 end end Definer.build_custom_class("Foo", "bar", "baz") f = Foo.new puts f.methods # => bar bar= baz baz=... 14. Framework does things for youFramework does things for you == awesome== awesome 15. Dont Repeat YourselfDont Repeat Yourself Convention Over ConfigurationConvention Over Configuration 16. Homebrew, XCodeHomebrew, XCode DatabaseDatabase Ruby Version Manager (RVM)Ruby Version Manager (RVM) Ruby, RailsRuby, Rails BundlerBundler GitGit Heroku ToolbeltHeroku Toolbelt 17. You are ready to build your first RailsYou are ready to build your first Rails application!application! 18. ruby.railstutorial.org/ruby-on-rails-tutorial-bookruby.railstutorial.org/ruby-on-rails-tutorial-book edgeguides.rubyonrails.orgedgeguides.rubyonrails.org tryruby.orgtryruby.org railsforzombies.orgrailsforzombies.org railscasts.comrailscasts.com github.com/rails/rails rails source codegithub.com/rails/rails rails source code github.com/semmin/intro-to-ror-preso thisgithub.com/semmin/intro-to-ror-preso this presentationpresentation 19. Arkin, McAnally, Ruby in Practice, Manning Publications, March 5, 2008Arkin, McAnally, Ruby in Practice, Manning Publications, March 5, 2008 Khells blog, khelll.com/blog/ruby/ruby-and-functional-programmingKhells blog, khelll.com/blog/ruby/ruby-and-functional-programming Stuart, www.rubyinside.com/functional-programming-in-ruby-2713.htmlStuart, www.rubyinside.com/functional-programming-in-ruby-2713.html 20. Thank You!Thank You!

Search related