the ruby idiom

42
The Ruby Idiom Ben Hughes @rubiety http://benhugh.es Friday, April 22, 2011

Upload: ben-hughes

Post on 26-Mar-2015

254 views

Category:

Documents


1 download

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

The Ruby Idiom

Ben Hughes@rubiety

http://benhugh.es

Friday, April 22, 2011

Page 2: The Ruby Idiom

Friday, April 22, 2011

Page 3: The Ruby Idiom

“Ruby is designed to make

programmers happy” - Matz

Friday, April 22, 2011

Page 4: The Ruby Idiom

Aesthetic EleganceVery IdiomaticLoose Syntax

Extremely DynamicExpressive

Friday, April 22, 2011

Page 5: The Ruby Idiom

Blocks

Friday, April 22, 2011

Page 6: The Ruby Idiom

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

open "people.txt" do |file| file.each_line do |line| puts line.split(",").first endend

Friday, April 22, 2011

Page 8: The Ruby Idiom

users.select {|u| u.age >= 21}.map(&:name).sort

Friday, April 22, 2011

Page 9: The Ruby Idiom

class Personend

Person.class # => Class

nil.class # => NilClasstrue.class # => TrueClass

Friday, April 22, 2011

Page 10: The Ruby Idiom

Open Classes

Friday, April 22, 2011

Page 11: The Ruby Idiom

class String def blank? self == "" endend

Friday, April 22, 2011

Page 12: The Ruby Idiom

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

4 + 2 # => 2

# Yes, Be Evil:class Numeric def +(another) self - another endend

Friday, April 22, 2011

Page 14: The Ruby Idiom

Executable Declarations

zsh$Friday, April 22, 2011

Page 15: The Ruby Idiom

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

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

@[email protected]

Friday, April 22, 2011

Page 17: The Ruby Idiom

Module Mix-ins

Friday, April 22, 2011

Page 18: The Ruby Idiom

module Jazz class Chord end class Scale endend

Jazz::Chord.newJazz::Scale.new

Friday, April 22, 2011

Page 19: The Ruby Idiom

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

Isn’t too much freedom a bad thing?

Friday, April 22, 2011

Page 21: The Ruby Idiom

In Theory? Yes.

In Practice? It’s not as bad as you

would think...

Friday, April 22, 2011

Page 22: The Ruby Idiom

Greater Good vs. Greater Evil

Solution Space (Good)

Evil

Friday, April 22, 2011

Page 23: The Ruby Idiom

Greater Good vs. Greater Evil

Solution Space (Good)

Pitfalls (Evil)

Friday, April 22, 2011

Page 24: The Ruby Idiom

Overlapping Safety

Safety/Confidence from Static

Typing

Friday, April 22, 2011

Page 25: The Ruby Idiom

Overlapping Safety

Safety from Static Typing

Safety/Confidence from Test Suite

Friday, April 22, 2011

Page 26: The Ruby Idiom

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

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

<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

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

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

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

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

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

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

C mmunity

Friday, April 22, 2011

Page 36: The Ruby Idiom

Cohesive & ActiveCommon Style/IdiomFocuses on Quality

Passionate About ToolsTesting Your Software

Friday, April 22, 2011

Page 37: The Ruby Idiom

Test Your Damn Software

Friday, April 22, 2011

Page 38: The Ruby Idiom

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

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

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

“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

Resources

• http://tryruby.org

• http://rubykoans.com

• http://sdruby.org

Ben Hughes@rubiety

http://benhugh.es

Friday, April 22, 2011