Successfully reported this slideshow.
Your SlideShare is downloading. ×

Ruby is an Acceptable Lisp

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Vim Script Programming
Vim Script Programming
Loading in …3
×

Check these out next

1 of 85 Ad

More Related Content

Slideshows for you (20)

Similar to Ruby is an Acceptable Lisp (20)

Advertisement

Recently uploaded (20)

Advertisement

Ruby is an Acceptable Lisp

  1. 1. Ruby is an acceptable Lisp Vitaly Kushner astrails.com
  2. 2. Ruby is better then Lisp
  3. 3. Ruby is better then Lisp sometimes* ;-)
  4. 4. Language
  5. 5. http://www.flickr.com/photos/jantik/254695220/
  6. 6. Power
  7. 7. http://www.flickr.com/photos/lrargerich/4187318085/
  8. 8. Blub Language Paradox Paul Graham http://paulgraham.com/avg.html
  9. 9. Is Lisp the most powerful?
  10. 10. NO
  11. 11. It depends
  12. 12. • syntax
  13. 13. • syntax • linguistic power
  14. 14. • syntax • linguistic power • domain
  15. 15. • syntax • linguistic power • domain • libraries
  16. 16. • syntax • linguistic power • domain • libraries • community
  17. 17. Ruby
  18. 18. History
  19. 19. History • Yukihiro Matsumoto (aka "Matz")
  20. 20. History • Yukihiro Matsumoto (aka "Matz") • Released in 1994
  21. 21. History • Yukihiro Matsumoto (aka "Matz") • Released in 1994 • Got known in US about 2000
  22. 22. History • Yukihiro Matsumoto (aka "Matz") • Released in 1994 • Got known in US about 2000 • Gained momentum around 2003-2005
  23. 23. more powerful than Perl more object-oriented than Python.
  24. 24. Perl Smalltalk Eiffel Ada Lisp
  25. 25. • Simple consistent syntax • Dynamically typed • Late binding • Single Inheritance with Mixin support
  26. 26. • Everything is an object • Closures • Garbage Collection • Multi platform
  27. 27. Ruby is Awesome
  28. 28. Clean Syntax
  29. 29. var # variable $var # global variable @var # instance variable @@var # class variable CONST # constant Class # class
  30. 30. attrs = { :src => "foo.img", :width => 100, :height => 200, :class => Avatar }
  31. 31. User.find params[:id], :limit => 10, :order => “name”
  32. 32. if @project.owned_by?(@user) return false unless @user.admin? end @project.complete!
  33. 33. Higher-order functions
  34. 34. Anonymous functions
  35. 35. def x_times(x, fun) for i in 1..x fun.call(i) end end x_times(10, lambda {|x| puts x})
  36. 36. def x_times(x) for i in 1..x yield i end end x_times(10) {|x| puts x}
  37. 37. def x_times(x) for i in 1..x yield i end end x_times(10) do |x| puts x end
  38. 38. Everything is an Object that you can extend
  39. 39. class Fixnum def x_times for i in 1..self yield i end end end 5.x_times { |x| puts x }
  40. 40. 5.times { |x| puts x }
  41. 41. map {|x| ...} collect {|x| ...} select {|x| ...} reject {|x| ...} find {|x| ...} any? {|x| ...} all? {|x| ...} sort {|a, b| ...}
  42. 42. 3.megabytes => 3145728
  43. 43. 10.months.from_now => Thu Aug 12 03:25:40 0300 2010
  44. 44. 5.minutes.ago => Mon Oct 12 03:21:02 0200 2009
  45. 45. Closures
  46. 46. def incrementor(increment) proc {|x| x + increment} end >> i5 = incrementor(5) => #<Proc:0x01874a78@(irb):46> >> i5.call(3) => 8
  47. 47. Compact
  48. 48. # ruby def paidMore(amount) proc {|e| e.salary > amount} end // C# public Predicate<Employee> PaidMore(int amount) { return delegate(Employee e) { return e.Salary > amount; } }
  49. 49. // ruby def foo(n) lambda {|i| n+=i} end ; lisp (defun foo (n) (lambda (i) (incf n i)))
  50. 50. # 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)))
  51. 51. Macros
  52. 52. Ruby fakes macros pretty well
  53. 53. OO + Monkey Patching
  54. 54. class Person def self.defsay(sound) define_method("say_#{sound}") do puts sound end end defsay :hello defsay :hi end
  55. 55. >> bob = Person.new => #<Person:0x185cba8> >> bob.say_hello hello => nil >> bob.say_hi hi => nil
  56. 56. 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
  57. 57. Magic
  58. 58. NoMethodError
  59. 59. method_missing
  60. 60. User.find_by_name_and_company( "Vitaly Kushner", "Astrails")
  61. 61. Ruby rewrite
  62. 62. require 'pp' require 'parse_tree' require 'parse_tree_extensions' def print_ast(&block) pp block.to_sexp end
  63. 63. print_ast do puts "hello" end s(:iter, s(:call, nil, :proc, s(:arglist)), nil, s(:call, nil, :puts, s(:arglist, s(:str, "hello"))))
  64. 64. Multiple inheritance is EVIL :-)
  65. 65. Modules a.k.a. Mixins
  66. 66. module FlyHome def set_home @home = position end def fly_home fly(@home) end end
  67. 67. class Bird < Living include FlyHome def fly(direction) ... def position ... end class Airplane < Machine include FlyHome def fly(direction) ... def position ... end
  68. 68. Libraries
  69. 69. Rubygems
  70. 70. ➜

~

✗
sudo
gem
install
astrails‐safe Successfully
installed
astrails‐safe‐0.2.7 1
gem
installed Installing
ri
documentation
for
astrails‐safe‐0.2.7... Building
YARD
(yri)
index
for
astrails‐safe‐0.2.7... Installing
RDoc
documentation
for
astrails‐safe‐0.2.7... ➜

~

✗

  71. 71. Rubygems.org 12,798 gems
  72. 72. Github.com
  73. 73. Community attitude
  74. 74. Testing
  75. 75. TDD
  76. 76. TDD Test Driven Development
  77. 77. BDD
  78. 78. BDD Behavior Driven Development
  79. 79. Pressure
  80. 80. TATFT
  81. 81. TATFT Test All The Fucking Time
  82. 82. • Test::Unit • RSpec • Shoulda • Cucumber • Webrat
  83. 83. 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_success end
  84. 84. Ruby is Better then Lisp Q &A Vitaly Kushner astrails.com @astrails @vkushner

×