85
Ruby is an acceptable Lisp Vitaly Kushner astrails.com

Ruby is an Acceptable Lisp

Embed Size (px)

DESCRIPTION

Why ruby is a practical implementation of Lisp and why is a good replacement that actually can be used in production

Citation preview

Page 1: Ruby is an Acceptable Lisp

Ruby isan acceptable Lisp

Vitaly Kushnerastrails.com

Page 2: Ruby is an Acceptable Lisp

Ruby is betterthen Lisp

Page 3: Ruby is an Acceptable Lisp

Ruby is betterthen Lisp

sometimes*;-)

Page 4: Ruby is an Acceptable Lisp

Language

Page 6: Ruby is an Acceptable Lisp

Power

Page 8: Ruby is an Acceptable Lisp

Blub Language Paradox

Paul Grahamhttp://paulgraham.com/avg.html

Page 9: Ruby is an Acceptable Lisp

Is Lisp the mostpowerful?

Page 10: Ruby is an Acceptable Lisp

NO

Page 11: Ruby is an Acceptable Lisp

It depends

Page 12: Ruby is an Acceptable Lisp
Page 13: Ruby is an Acceptable Lisp

• syntax

Page 14: Ruby is an Acceptable Lisp

• syntax

• linguistic power

Page 15: Ruby is an Acceptable Lisp

• syntax

• linguistic power

• domain

Page 16: Ruby is an Acceptable Lisp

• syntax

• linguistic power

• domain

• libraries

Page 17: Ruby is an Acceptable Lisp

• syntax

• linguistic power

• domain

• libraries

• community

Page 18: Ruby is an Acceptable Lisp

Ruby

Page 19: Ruby is an Acceptable Lisp

History

Page 20: Ruby is an Acceptable Lisp

History

• Yukihiro Matsumoto (aka "Matz")

Page 21: Ruby is an Acceptable Lisp

History

• Yukihiro Matsumoto (aka "Matz")

• Released in 1994

Page 22: Ruby is an Acceptable Lisp

History

• Yukihiro Matsumoto (aka "Matz")

• Released in 1994

• Got known in US about 2000

Page 23: Ruby is an Acceptable Lisp

History

• Yukihiro Matsumoto (aka "Matz")

• Released in 1994

• Got known in US about 2000

• Gained momentum around 2003-2005

Page 24: Ruby is an Acceptable Lisp

more powerfulthan Perl

more object-oriented than Python.

Page 25: Ruby is an Acceptable Lisp

PerlSmalltalk

EiffelAdaLisp

Page 26: Ruby is an Acceptable Lisp

• Simple consistent syntax

• Dynamically typed

• Late binding

• Single Inheritance with Mixin support

Page 27: Ruby is an Acceptable Lisp

• Everything is an object

• Closures

• Garbage Collection

• Multi platform

Page 28: Ruby is an Acceptable Lisp

Ruby is Awesome

Page 29: Ruby is an Acceptable Lisp

Clean Syntax

Page 30: Ruby is an Acceptable Lisp

var # variable$var # global variable@var # instance variable@@var # class variableCONST # constantClass # class

Page 31: Ruby is an Acceptable Lisp

attrs = { :src => "foo.img", :width => 100, :height => 200, :class => Avatar}

Page 32: Ruby is an Acceptable Lisp

User.find params[:id], :limit => 10, :order => “name”

Page 33: Ruby is an Acceptable Lisp

if @project.owned_by?(@user) return false unless @[email protected]!

Page 34: Ruby is an Acceptable Lisp

Higher-orderfunctions

Page 35: Ruby is an Acceptable Lisp

Anonymousfunctions

Page 36: Ruby is an Acceptable Lisp

def x_times(x, fun) for i in 1..x fun.call(i) endend

x_times(10, lambda {|x| puts x})

Page 37: Ruby is an Acceptable Lisp

def x_times(x) for i in 1..x yield i endend

x_times(10) {|x| puts x}

Page 38: Ruby is an Acceptable Lisp

def x_times(x) for i in 1..x yield i endend

x_times(10) do |x| puts xend

Page 39: Ruby is an Acceptable Lisp

Everything isan Object

that you can extend

Page 40: Ruby is an Acceptable Lisp

class Fixnum def x_times for i in 1..self yield i end endend

5.x_times { |x| puts x }

Page 41: Ruby is an Acceptable Lisp

5.times { |x| puts x }

Page 42: Ruby is an Acceptable Lisp

map {|x| ...}collect {|x| ...}select {|x| ...}reject {|x| ...}find {|x| ...}any? {|x| ...}all? {|x| ...}sort {|a, b| ...}

Page 43: Ruby is an Acceptable Lisp

3.megabytes=> 3145728

Page 44: Ruby is an Acceptable Lisp

10.months.from_now

=> Thu Aug 12 03:25:40 0300 2010

Page 45: Ruby is an Acceptable Lisp

5.minutes.ago

=> Mon Oct 12 03:21:02 0200 2009

Page 46: Ruby is an Acceptable Lisp

Closures

Page 47: Ruby is an Acceptable Lisp

def incrementor(increment) proc {|x| x + increment}end

>> i5 = incrementor(5)=> #<Proc:0x01874a78@(irb):46>>> i5.call(3)=> 8

Page 48: Ruby is an Acceptable Lisp

Compact

Page 49: Ruby is an Acceptable Lisp

# rubydef paidMore(amount) proc {|e| e.salary > amount}end

// C#public Predicate<Employee> PaidMore(int amount) { return delegate(Employee e) { return e.Salary > amount; } }

Page 50: Ruby is an Acceptable Lisp

// rubydef foo(n) lambda {|i| n+=i} end

; lisp(defun foo (n) (lambda (i) (incf n i)))

Page 51: Ruby is an Acceptable Lisp

# ruby[1,2,3].map {|n| n*n }.reject {|n| n%3==1 }

; lisp(remove-if (lambda (n) (= (mod n 3) 1)) (mapcar (lambda (n) (* n n)) '(1 2 3)))

Page 52: Ruby is an Acceptable Lisp

Macros

Page 53: Ruby is an Acceptable Lisp

Ruby fakes macrospretty well

Page 54: Ruby is an Acceptable Lisp

OO+

Monkey Patching

Page 55: Ruby is an Acceptable Lisp

class Person def self.defsay(sound) define_method("say_#{sound}") do puts sound end end

defsay :hello defsay :hiend

Page 56: Ruby is an Acceptable Lisp

>> bob = Person.new=> #<Person:0x185cba8>>> bob.say_hellohello=> nil>> bob.say_hihi=> nil

Page 57: Ruby is an Acceptable Lisp

class Plugin < ActiveRecord::Base validates_presence_of :name validates_presence_of :description validates_presence_of :author_id belongs_to :author, :class_name => "User" has_many :plugin_versions, :dependent => :destroy belongs_to :default_version, :class_name => "PluginVersion" acts_as_commentable acts_as_taggable ...end

Page 58: Ruby is an Acceptable Lisp

Magic

Page 59: Ruby is an Acceptable Lisp

NoMethodError

Page 60: Ruby is an Acceptable Lisp

method_missing

Page 61: Ruby is an Acceptable Lisp

User.find_by_name_and_company("Vitaly Kushner", "Astrails")

Page 62: Ruby is an Acceptable Lisp

Ruby rewrite

Page 63: Ruby is an Acceptable Lisp

require 'pp'require 'parse_tree'require 'parse_tree_extensions'

def print_ast(&block) pp block.to_sexpend

Page 64: Ruby is an Acceptable Lisp

print_ast do puts "hello" end

s(:iter, s(:call, nil, :proc, s(:arglist)), nil, s(:call, nil, :puts, s(:arglist, s(:str, "hello"))))

Page 65: Ruby is an Acceptable Lisp

Multiple inheritanceis EVIL

:-)

Page 66: Ruby is an Acceptable Lisp

Modulesa.k.a. Mixins

Page 67: Ruby is an Acceptable Lisp

module FlyHome def set_home @home = position end

def fly_home fly(@home) endend

Page 68: Ruby is an Acceptable Lisp

class Bird < Living include FlyHome def fly(direction) ... def position ...end

class Airplane < Machine include FlyHome def fly(direction) ... def position ...end

Page 69: Ruby is an Acceptable Lisp

Libraries

Page 70: Ruby is an Acceptable Lisp

Rubygems

Page 71: Ruby is an Acceptable Lisp

➜~✗sudogeminstallastrails‐safeSuccessfullyinstalledastrails‐safe‐0.2.71geminstalledInstallingridocumentationforastrails‐safe‐0.2.7...BuildingYARD(yri)indexforastrails‐safe‐0.2.7...InstallingRDocdocumentationforastrails‐safe‐0.2.7...➜~✗

Page 72: Ruby is an Acceptable Lisp

Rubygems.org12,798 gems

Page 73: Ruby is an Acceptable Lisp

Github.com

Page 74: Ruby is an Acceptable Lisp

Communityattitude

Page 75: Ruby is an Acceptable Lisp

Testing

Page 76: Ruby is an Acceptable Lisp

TDD

Page 77: Ruby is an Acceptable Lisp

TDDTest Driven Development

Page 78: Ruby is an Acceptable Lisp

BDD

Page 79: Ruby is an Acceptable Lisp

BDDBehavior Driven Development

Page 80: Ruby is an Acceptable Lisp

Pressure

Page 81: Ruby is an Acceptable Lisp

TATFT

Page 82: Ruby is an Acceptable Lisp

TATFTTest All The Fucking Time

Page 83: Ruby is an Acceptable Lisp

• Test::Unit

• RSpec

• Shoulda

• Cucumber

• Webrat

Page 84: Ruby is an Acceptable Lisp

it "should be able to show media" do @media = stub_media Media.stub!(:find).and_return(@media) get :show, :id => @media.id response.should be_successend

Page 85: Ruby is an Acceptable Lisp

Ruby is Better then Lisp

Q & AVitaly Kushnerastrails.com

@astrails @vkushner