the ruby idiom
DESCRIPTION
An overview of the unique features of the Ruby language and why Rubyists are so passionate about their tools.TRANSCRIPT
![Page 1: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/1.jpg)
The Ruby Idiom
Ben Hughes@rubiety
http://benhugh.es
Friday, April 22, 2011
![Page 2: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/2.jpg)
Friday, April 22, 2011
![Page 3: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/3.jpg)
“Ruby is designed to make
programmers happy” - Matz
Friday, April 22, 2011
![Page 4: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/4.jpg)
Aesthetic EleganceVery IdiomaticLoose Syntax
Extremely DynamicExpressive
Friday, April 22, 2011
![Page 5: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/5.jpg)
Blocks
Friday, April 22, 2011
![Page 6: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/6.jpg)
people.each do |person| puts person.nameend
1.upto(10) do |i| puts "Counting #{i}"end
5.times { puts "Hello" }
Friday, April 22, 2011
![Page 7: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/7.jpg)
open "people.txt" do |file| file.each_line do |line| puts line.split(",").first endend
Friday, April 22, 2011
![Page 8: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/8.jpg)
users.select {|u| u.age >= 21}.map(&:name).sort
Friday, April 22, 2011
![Page 9: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/9.jpg)
class Personend
Person.class # => Class
nil.class # => NilClasstrue.class # => TrueClass
Friday, April 22, 2011
![Page 10: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/10.jpg)
Open Classes
Friday, April 22, 2011
![Page 11: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/11.jpg)
class String def blank? self == "" endend
Friday, April 22, 2011
![Page 12: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/12.jpg)
class Numeric def seconds self end def minutes self * 60 end def hours self * 3600 end def from_now Time.now + self endend
12.hours.from_now
2.minutes + 30.seconds
Friday, April 22, 2011
![Page 13: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/13.jpg)
4 + 2 # => 2
# Yes, Be Evil:class Numeric def +(another) self - another endend
Friday, April 22, 2011
![Page 14: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/14.jpg)
Executable Declarations
zsh$Friday, April 22, 2011
![Page 15: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/15.jpg)
class Person def name @name end if Superpowers.enabled? def give_superpowers @ability = "Fly" end endend
Friday, April 22, 2011
![Page 16: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/16.jpg)
class Model def self.acts_as_tree define_method :parent do find(parent_id) end define_method :children do where(:parent_id => self.id) end endend
class Category < Model acts_as_treeend
Friday, April 22, 2011
![Page 17: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/17.jpg)
Module Mix-ins
Friday, April 22, 2011
![Page 18: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/18.jpg)
module Jazz class Chord end class Scale endend
Jazz::Chord.newJazz::Scale.new
Friday, April 22, 2011
![Page 19: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/19.jpg)
module FilePersistence def load_from(path) endend
class MusicCollection include Enumerable include FilePersistenceend
@collection = [email protected]_from "songs"@collection.each do |song| song.playend
Friday, April 22, 2011
![Page 20: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/20.jpg)
Isn’t too much freedom a bad thing?
Friday, April 22, 2011
![Page 21: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/21.jpg)
In Theory? Yes.
In Practice? It’s not as bad as you
would think...
Friday, April 22, 2011
![Page 22: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/22.jpg)
Greater Good vs. Greater Evil
Solution Space (Good)
Evil
Friday, April 22, 2011
![Page 23: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/23.jpg)
Greater Good vs. Greater Evil
Solution Space (Good)
Pitfalls (Evil)
Friday, April 22, 2011
![Page 24: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/24.jpg)
Overlapping Safety
Safety/Confidence from Static
Typing
Friday, April 22, 2011
![Page 25: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/25.jpg)
Overlapping Safety
Safety from Static Typing
Safety/Confidence from Test Suite
Friday, April 22, 2011
![Page 26: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/26.jpg)
Domain-Specific Languages
• Sub-Languages of Ruby for particular purposes
• High-Level Abstractions
• Captures Knowledge in an Extremely Readable Way
Friday, April 22, 2011
![Page 27: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/27.jpg)
Markaby::Builder.new.html do head { title "Boats.com" } body do h1 "Boats.com has great deals" ul.specials do li "$49 for a canoe" li "$39 for a raft" li "$29 for a huge boat" end endend
Friday, April 22, 2011
![Page 28: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/28.jpg)
<html> <head><title>Boats.com</title></head> <body> <h1>Boats.com has great deals</h1> <ul class="specials"> <li>$49 for a canoe</li> <li>$39 for a raft</li> <li>$29 for a huge boat</li> </ul> </body></html>
Friday, April 22, 2011
![Page 29: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/29.jpg)
class Payment state_machine :state, :initial => :new do event :authorize do transition :new => :authorized end event :capture do transition :authorized => :captured end before_transition any => :authorize, :do => :authorize_card before_transition any => :capture, :do => :capture_card endend
@payment = [email protected] # => "new"@[email protected]!
@payment.captured? # => trueFriday, April 22, 2011
![Page 30: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/30.jpg)
class Comment < ActiveRecord::Base belongs_to :article validates :name, :email, :comment, :presence => true def approve! update_attribute :approved, true endend
class Article < ActiveRecord::Base has_many :comments scope :published, where(published: true) validates :title, :presence => true validates :content, :length => { minimum: 50 }end
Friday, April 22, 2011
![Page 31: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/31.jpg)
class User < ActiveRecord::Base has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }end
@user.avatar.url(:medium)
<% form_for(@user) do |f| %> <%= f.file_field :avatar %><% end %>
Friday, April 22, 2011
![Page 32: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/32.jpg)
Chord['Ebmaj7'].notesChord['maj7'].in_key_of('Eb').notes# => ['Eb', 'G', 'Bb', 'D']
Scale['Whole Tone'].notes# => ['C', 'D', 'E', 'F#', 'G#', 'Bb']
Scale['Major'].in_key_of('Eb').modes['Dorian'].notes# => ['F', 'G', 'Ab', 'Bb', 'C', 'D', 'Eb']
Scale['Major']['Dorian'].chords.symbols# => ['min7', 'min6']
Chord['Amin7'].modes.names# => ['A Dorian']
Friday, April 22, 2011
![Page 33: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/33.jpg)
every 3.hours do command "/usr/bin/my_great_command"end
every 1.day, :at => ['4:30 am', '10:30 am'] do runner "MyModel.task_to_run"end
every :hour do runner "SomeModel.ladeeda"end
every :sunday, :at => '12pm' do runner "Task.do_something_great"end
Friday, April 22, 2011
![Page 34: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/34.jpg)
Person.joins { articles.comments }.where {{ articles.comments => ( id.in([1,2,3]) | body.matches('First post!%') )}}.to_sql
=> SELECT "people".* FROM "people" INNER JOIN "articles" ON "articles"."person_id" = "people"."id" INNER JOIN "comments" ON "comments"."article_id" = "articles"."id" WHERE (("comments"."id" IN (1, 2, 3) OR "comments"."body" LIKE 'First post!%'))
Friday, April 22, 2011
![Page 35: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/35.jpg)
C mmunity
Friday, April 22, 2011
![Page 36: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/36.jpg)
Cohesive & ActiveCommon Style/IdiomFocuses on Quality
Passionate About ToolsTesting Your Software
Friday, April 22, 2011
![Page 37: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/37.jpg)
Test Your Damn Software
Friday, April 22, 2011
![Page 38: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/38.jpg)
describe User do before do @user = User.create(:login => "joe", :password => "pw") end it "should be invalid without username" do @user.username = "" @user.should_not be_valid @user.should have(1).error_on(:username) end it "should destroy" do expect { @user.destroy }.to change { User.count }.by(-1) endend
Friday, April 22, 2011
![Page 39: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/39.jpg)
describe Key do it "should default to C" do Key.default.name.should == "C" end it "should expose 12 primary keys" do Key.should have(12).primaries end it "should treat Eb and D# as enharmonic" do Key['Eb'].should be_enharmonic_with(Key['D#']) endend
Friday, April 22, 2011
![Page 40: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/40.jpg)
It’s a great time to learn Ruby
• Rails 3 recently released - very solid!
• Ruby 1.9 - Runs on a proper VM and very fast
• Bundler / RVM for Ruby and Gem dependencies
• Other Rubies maturing: JRuby, MacRuby, Rubinius
• Deployment easy with mod_rails or Heroku
• Strong community with resources/help
Friday, April 22, 2011
![Page 41: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/41.jpg)
“Ruby fits my brain like a glove”
- David Heinemeier Hansson
“Power with clarity and choice”
- Glenn Vanderburg
Friday, April 22, 2011
![Page 42: The Ruby Idiom](https://reader031.vdocuments.site/reader031/viewer/2022013114/551567c1497959021e8b4cba/html5/thumbnails/42.jpg)
Resources
• http://tryruby.org
• http://rubykoans.com
• http://sdruby.org
Ben Hughes@rubiety
http://benhugh.es
Friday, April 22, 2011