RubyBudapest - 7th June, 2012       Papp László     nucc @ t witter
The first met...                                                 blog.bteam.huOpen Academy - June 7, 2012                 ...
Yukihiro Matsumoto                                     (matz)                                                   blog.bteam...
"I wanted a scripting language that         was more powerful than Perl, and         more object-oriented than Python.    ...
R ecipe                              120g  Lisp                              100g  Perl                               130g...
Interpreted                                     Dynamic typed                         Strong in meta-programming          ...
blog.bteam.huOpen Academy - June 7, 2012   twitter.com/nucc
Ruby                                     blog.bteam.huOpen Academy - June 7, 2012          twitter.com/nucc
“Trying to make Ruby natural,                      not simple”                                       blog.bteam.huOpen Aca...
“Ruby is simple in appearance,                     but is very complex inside,                      just like our human bo...
People.say(“Hello OpenAcademy!”)                                          blog.bteam.huOpen Academy - June 7, 2012        ...
People.say “Hello OpenAcademy!”                                          blog.bteam.huOpen Academy - June 7, 2012         ...
trash << file if file.rubbish?                                          blog.bteam.huOpen Academy - June 7, 2012          ...
trash << file unless file.referenced?                                     blog.bteam.huOpen Academy - June 7, 2012        ...
i                     f      trash << file unless file.referenced?                     o                     t            ...
:Symbol                                        blog.bteam.huOpen Academy - June 7, 2012             twitter.com/nucc
prezi = {                                  :title => “Ruby”,                                  :date => “07/06/12”         ...
Academy.new     :location => “BME”                                             blog.bteam.huOpen Academy - June 7, 2012   ...
Academy.new({:location => “BME”})                                          blog.bteam.huOpen Academy - June 7, 2012       ...
(1..100).select do |i|                                  return i % 2 == 0                          end                    ...
(1..100).select do |i|                                  i % 2 == 0                          end                          =...
(1..100).select {|i| i % 2 == 0}                                        blog.bteam.huOpen Academy - June 7, 2012          ...
def repeat(x, &block)              (1..x).each do |i|                yield(i)              end            end             ...
def repeat(x, &block)              (1..x).each do |i|                yield(i)              end            end             ...
def repeat(x, &block)              (1..x).each do |i|                yield(i)              end            end             ...
blog.bteam.huOpen Academy - June 7, 2012   twitter.com/nucc
Everything is an object!                               Really!                                                  blog.bteam...
1                                  blog.bteam.huOpen Academy - June 7, 2012       twitter.com/nucc
1.even?                              => false                                         blog.bteam.huOpen Academy - June 7, ...
5.times do                     puts “I will never use PERL!”          end                                              blo...
1.nil?                              => false                                         blog.bteam.huOpen Academy - June 7, 2...
nil                                    blog.bteam.huOpen Academy - June 7, 2012         twitter.com/nucc
nil.nil?                              => true                                         blog.bteam.huOpen Academy - June 7, ...
openAcademy = Class.new                      thisAcademy = openAcademy.new                                                ...
class OpenAcademy             def initialize(year, month)               @year = year               @month = month         ...
class OpenAcademy             attr :year             def initialize(year, month)               @year = year               ...
class OpenAcademy             attr :year       end                              def year                                @y...
class OpenAcademy             attr_accessor :year       end                    def year                                @ye...
class OpenAcademy             attr_accessor :year       end                              academy = OpenAcademy.new(2011, 0...
class OpenAcademy             attr_accessor :year             def year=(y)               @year = y.to_s             end   ...
class OpenAcademy             attr_accessor :year             def year=(y)               @year = y.to_s             end   ...
class Academy < Conference                      end                                                   blog.bteam.huOpen Ac...
module Comperable                                # mixin                              end                                 ...
class Academy < Conference     include Comperable     def <=>(o)       myval = self.year.to_s + self.month.to_s       othe...
class Academy < Conference     include Comperable     def <=>(o)       myval = self.year.to_s + self.month.to_s       othe...
blog.bteam.huOpen Academy - June 7, 2012   twitter.com/nucc
Advanced Level                                blog.bteam.huOpen Academy - June 7, 2012     twitter.com/nucc
2.prim?                                        blog.bteam.huOpen Academy - June 7, 2012             twitter.com/nucc
class Fixnum     def prim?       2.upto(to_i/2).each do |i|         return false if to_i % i == 0       end       to_i > 1...
class Fixnum     def prim?       2.upto(to_i/2).each do |i|         return false if to_i % i == 0       end       to_i > 1...
class << my_object                                def prim?                                  ...                          ...
academy = Mock.new             academy.stubs(:year).returns(2012)             academy.stubs(:month).returns(6)            ...
academy = Mock.new             academy.stubs(:year).returns(2012)             academy.stubs(:month).returns(6)            ...
class Xml   def method_missing(method, *args, &block)     print "<#{method}>"     yield if block     print "</#{method}>" ...
class User      %w(name email password).each do |method|        define_method(method) do          @data[method.to_sym]    ...
object.methods                       object.respond_to? :method                       alias :from, :to                    ...
Dave Thomas - Programming Ruby                                          blog.bteam.huOpen Academy - June 7, 2012          ...
?                              twitter.com/nucc                                                 blog.bteam.huOpen Academy ...
Upcoming SlideShare
Loading in …5
×

Open Academy - Ruby

597 views

Published on

Published in: Technology
  • Be the first to comment

Open Academy - Ruby

  1. 1. RubyBudapest - 7th June, 2012 Papp László nucc @ t witter
  2. 2. The first met... blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  3. 3. Yukihiro Matsumoto (matz) blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  4. 4. "I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. Thats why I decided to design my own language." blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  5. 5. R ecipe 120g Lisp 100g Perl 130g P ython g Sm alltalk 120 blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  6. 6. Interpreted Dynamic typed Strong in meta-programming blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  7. 7. blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  8. 8. Ruby blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  9. 9. “Trying to make Ruby natural, not simple” blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  10. 10. “Ruby is simple in appearance, but is very complex inside, just like our human body” blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  11. 11. People.say(“Hello OpenAcademy!”) blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  12. 12. People.say “Hello OpenAcademy!” blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  13. 13. trash << file if file.rubbish? blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  14. 14. trash << file unless file.referenced? blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  15. 15. i f trash << file unless file.referenced? o t blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  16. 16. :Symbol blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  17. 17. prezi = { :title => “Ruby”, :date => “07/06/12” } blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  18. 18. Academy.new :location => “BME” blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  19. 19. Academy.new({:location => “BME”}) blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  20. 20. (1..100).select do |i| return i % 2 == 0 end => [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100] blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  21. 21. (1..100).select do |i| i % 2 == 0 end => [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100] blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  22. 22. (1..100).select {|i| i % 2 == 0} blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  23. 23. def repeat(x, &block) (1..x).each do |i| yield(i) end end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  24. 24. def repeat(x, &block) (1..x).each do |i| yield(i) end end repeat 10 do puts “Hello Academy!” end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  25. 25. def repeat(x, &block) (1..x).each do |i| yield(i) end end repeat 10 do |iteration| puts “#{iteration}. Hello Academy!” end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  26. 26. blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  27. 27. Everything is an object! Really! blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  28. 28. 1 blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  29. 29. 1.even? => false blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  30. 30. 5.times do puts “I will never use PERL!” end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  31. 31. 1.nil? => false blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  32. 32. nil blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  33. 33. nil.nil? => true blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  34. 34. openAcademy = Class.new thisAcademy = openAcademy.new blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  35. 35. class OpenAcademy def initialize(year, month) @year = year @month = month end end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  36. 36. class OpenAcademy attr :year def initialize(year, month) @year = year @month = month end end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  37. 37. class OpenAcademy attr :year end def year @year end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  38. 38. class OpenAcademy attr_accessor :year end def year @year end def year=(value) @year = value end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  39. 39. class OpenAcademy attr_accessor :year end academy = OpenAcademy.new(2011, 06) academy.year = 2012 puts academy.year => 2012 blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  40. 40. class OpenAcademy attr_accessor :year def year=(y) @year = y.to_s end end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  41. 41. class OpenAcademy attr_accessor :year def year=(y) @year = y.to_s end academy = OpenAcademy.new(2011, 06) end academy.year = 2012 puts academy.year => “2012” blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  42. 42. class Academy < Conference end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  43. 43. module Comperable # mixin end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  44. 44. class Academy < Conference include Comperable def <=>(o) myval = self.year.to_s + self.month.to_s other = o.year.to_s + o.month.to_s myval <=> other end end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  45. 45. class Academy < Conference include Comperable def <=>(o) myval = self.year.to_s + self.month.to_s other = o.year.to_s + o.month.to_s myval <=> other end end this_academy = Academy.new(2012, 6) last_academy = Academy.new(2011, 10) last_academy < this_academy => true blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  46. 46. blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  47. 47. Advanced Level blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  48. 48. 2.prim? blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  49. 49. class Fixnum def prim? 2.upto(to_i/2).each do |i| return false if to_i % i == 0 end to_i > 1 endend blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  50. 50. class Fixnum def prim? 2.upto(to_i/2).each do |i| return false if to_i % i == 0 end to_i > 1 end 2.prim? => trueend 1.prim? => false blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  51. 51. class << my_object def prim? ... end end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  52. 52. academy = Mock.new academy.stubs(:year).returns(2012) academy.stubs(:month).returns(6) blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  53. 53. academy = Mock.new academy.stubs(:year).returns(2012) academy.stubs(:month).returns(6) print academy.month => 6 # Test confererence registration Conferences.register academy blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  54. 54. class Xml def method_missing(method, *args, &block) print "<#{method}>" yield if block print "</#{method}>" end end doc = Xml.new doc.head { doc.title { print “Academy } } => <head><title>Academy</title></head> blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  55. 55. class User %w(name email password).each do |method| define_method(method) do @data[method.to_sym] end end end blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  56. 56. object.methods object.respond_to? :method alias :from, :to class_eval module_eval Module.included(base) ... blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  57. 57. Dave Thomas - Programming Ruby blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc
  58. 58. ? twitter.com/nucc blog.bteam.huOpen Academy - June 7, 2012 twitter.com/nucc

×