Ruby Presentation - Article
Upcoming SlideShare
Loading in...5
×
 

Ruby Presentation - Article

on

  • 256 views

 

Statistics

Views

Total Views
256
Views on SlideShare
255
Embed Views
1

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 1

http://localhost 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Ruby Presentation - Article Ruby Presentation - Article Document Transcript

  • Ruby: Why We Love Ithttps://github.com/Kelsin/ruby-presentationChristopher GiroirNovember 8th, 2011Contents1 The Language 11.1 Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Why We Love It . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.3 Gems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Tools 62.1 Bundler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.2 RVM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 The Language1.1 BasicsOverview• Inspired by Smalltalk (which I love)• Also draws from Perl, Eiffel, Ada and LISP• Includes a REPL• Built for developers as a language they would love to use• Dynamic, strict, reflective, object oriented• Everything is an expression (even statements)• Everything is executed imperatively (even declarations)Object Oriented• Everything is an object• Single Inheritance• Modules can be mixed in• Dynamic Dispatch1
  • Simple Code1 5.times { print "Hello" }This outputs:1 Hello2 Hello3 Hello4 Hello5 Hello6 => 5Types1 # Strings2 s = ’Testing’34 # Interpreted Strings5 t = "Double #{str}"67 # Symbols8 sym = :chris910 # Arrays11 a = [1,2,3]1213 # Hashes14 h = { :key => ’value’, :chris => ’awesome’ }Classes1 class Box2 def initialize(w,h,d)3 @width = w4 @height = h5 @depth = d6 end78 def volume9 @width * @height * @depth10 end11 end1213 box = Box.new(2,2,2)14 box.volume # => 8Simple Inheritance1 class JackInTheBox < Box2 def initialize(msg)3 @msg = msg2
  • 4 super(3,3,3)5 end67 def open8 puts @msg9 end10 end1112 jbox = JackInTheBox.new(’Surprise!’)13 jbox.volume # => 2714 jbox.open # prints ’Surprise!’Control1 while true == false2 if var == 53 break4 end56 begin7 var - 18 end while var < 4910 next if var == 611 endBlocks1 [1,2,3].each { |n| puts n }This outputs:1 12 23 34 => [1,2,3]Block Syntax1 5.upto(10) { |n| puts n }This is exactly the same as the following:1 5.upto(10) do |n|2 puts n3 end1.2 Why We Love ItAttribute Methods3
  • 1 class Person2 def name3 @name4 end5 def social=(s)6 @social = s7 end8 def age9 @age10 end11 def age=(a)12 @age = a13 end14 endThe Easy Way1 class Person2 attr_reader :name3 attr_writer :social4 attr_accessor :age5 endThe Easy Way Explained1 class Person2 attr_reader :name3 attr_writer :social4 attr_accessor :age5 end• Ruby syntax allows method calls without ()• Result is clean and looks like a language feature• We can implement this ourselves• Untested code, please do not copy:1 class Object2 def self.attr_reader(var)3 class_eval <<-METHODS4 def #{var}5 @#{var}6 end7 METHODS8 end9 endWhy Blocks1 (map (lambda (n)2 (+ n 5))3 ’(1 2 3))4
  • Becomes:1 [1,2,3].map do |n|2 n + 53 endResults in:1 => [6,7,8]1.3 GemsModules1 module Voice2 def say(msg)3 puts msg4 end5 end67 class Person8 include Voice9 end1011 p = Person.new12 p.say(’Hello’) # prints ’Hello’Using GemsRequire loads in files1 require ’saver’ # pulls in ’saver.rb’Gems allow us to not deal with paths1 require ’rubygems’2 require ’saver’34 class Item5 include Saver6 endWriting Gems1 Gem::Specification.new do |s|2 s.name = "saver"3 s.version = Saver::VERSION4 s.authors = ["Christopher Giroir"]5 s.email = ["kelsin@valefor.com"]6 s.homepage = "http://kelsin.github.com/saver/"78 s.files = ‘git ls-files‘.split("n")9 s.require_paths = ["lib"]1011 s.add_dependency ’activesupport’, ’~> 3.0.0’12 s.add_dependency ’mongo_mapper’13 end5
  • 2 Tools2.1 BundlerWhy Bundler?• Many projects (i.e. rails apps) are not gems themselves• They do have gem dependencies• Easy way to install and keep track of these dependencies• Making sure ONLY the proper gems are usedThe Gemfile1 source ’http://tools1.savewave.com/rubygems’2 source ’http://rubygems.org’34 gem ’rails’, ’3.0.7’56 gem ’sw-model’, ’0.13.0’78 group :development, :test do9 gem "rspec"10 endUsing Bundler1 # Install the gems from the Gemfile2 bundle install34 # Update gems to new versions5 bundle update67 # Execute command with proper gems8 bundle exec rake specIn your ruby code1 require "rubygems"2 require "bundler/setup"3 require "saver"Gemfile.lock• When you initially install versions are saved to Gemfile.lock• After they are only updated on bundle update• SHOULD be checked into version control• Protects from version updates6
  • 2.2 RVMWhy RVM?• Different projects might use different versions of rails• Different projects might use different ruby interpreters– Ruby– JRuby– Rubinus• While bundler helps, complete gem isolation is better!• It’s nice to keep your system ruby separate and not update itUsing RVM1 # Install the default 1.9.2 ruby interpretor2 rvm install 1.9.234 # Switch to using 1.9.25 rvm use 1.9.267 # List installed rubies8 rvm listRVM Gemsets1 # Create a new gemset2 rvm gemset create savingstar-web34 # List gemsets5 rvm gemset list67 # Switch to a ruby and gemset together8 rvm use 1.9.2@savingstar-web.rvmrc• A .rvmrc file per project allows you to say which ruby and gemset to use• Should be in source control. Helps RVM users out, ignored for others• It’s a shell script that’s executed everytime you cd (very unsafe)• Makes life very easy however!1 rvm use 1.9.2@saveingstar-web --create7