Save 37% off PRO during our Black Friday Sale! »

Ruby - the most interesting language in the world

Ruby - the most interesting language in the world

Intro to Ruby Syntax (accompanies 7 Languages in 7 Weeks) @ Houston Open Development Group

3b5cb0411cbd1012acab1b60f7ce3606?s=128

Jesse Wolgamott

March 07, 2013
Tweet

Transcript

  1. 3VCZ +FTTF8PMHBNPUU!KXP )PVTUPO0QFO%FW .BSDI  Friday, March 8, 13

  2. UIFNPTUJOUFSFTUJOHMBOHVBHFJOUIFXPSME Friday, March 8, 13

  3. 1SFBNCMF UIFPWFSWJFX Friday, March 8, 13

  4. 3VCZ%JWJEFEJOUPUISFFTFDUJPOT 0CKFDU0SJFOUFE 4ZTUFN)BDLJOH 1FSM4DSJQUJOH Friday, March 8, 13

  5. )JTUPSZ $SFBUFECZ:VLJIJSP.BUTVNPUPJO  NBU[ )FZ UIBUTZFBSTBHP ZBZ Friday, March 8,

    13
  6. .BU[!-43$ Friday, March 8, 13

  7. CUX #ZUIF8BZ NBU[JTOJDF .*/*48"/ .BU[JT/JDF 4P8F"SF/JDF Friday, March 8, 13

  8. )JTUPSZ$POUJOVFE 8BTEJTDPWFSFEJOFBSMZTCZ%BWF 5IPNBT $BVHIUPOXJUIFBSMZBEPQUFSTUSZJOHUP NPWFBXBZGSPN1FSM $BVHIUpSFXJUI%BWJE)FJOFNFJIFS )BOTPOT3BJMTJO Friday, March 8,

    13
  9. 1MBOPG"UUBDL 8FXJMMPOMZDPWFS00QBSUPGSVCZ  TJODFJUTUIFPOMZQBSUBOZPOFVTFT 4ZOUBYBOE5IFPSZ #SFBL -JWF$PEJOH Friday, March 8,

    13
  10. 4ZOUBY Friday, March 8, 13

  11. $MBTTFT class Page end class Author end Friday, March 8,

    13
  12. .FUIPET def name “hello world” end >> name => “hello

    world” Friday, March 8, 13
  13. $POTUBOUT class MyClass MAXIMUM = 5 def max MAXIMUM end

    end MyClass::MAXIMUM => 5 MyClass.new.max => 5 Friday, March 8, 13
  14. $BTF4XJUDI def do_things(object) case object.status when :active # something when

    :almost_active # something when :inactive # something else # something else end end Friday, March 8, 13
  15. &YDFQUJPOT File.read('/usr/var/non_existent') Errno::ENOENT: No such file or directory - /usr/

    var/non_existent ! from (irb):41:in `read' def read File.read('/usr/var/non_existent') rescue Errno::ENOENT puts 'hey, file not found' end >> read hey, file not found => nil Friday, March 8, 13
  16. .FUIPE1BSBNFUFST "SHVNFOUT def say_things(words) puts words end >> say_thing “yo!”

    yo => nil Friday, March 8, 13
  17. 7BSJBCMFT class Person def say_hi name = "Peter" "hi #{name}"

    end end >> Person.new.say_hi Friday, March 8, 13
  18. 7BSJBCMFT class Person def say_hi name = "Peter" "hi #{name}"

    end end >> Person.new.say_hi => "hi Peter" Friday, March 8, 13
  19. 7BSJBCMFT BSFMPDBMTDPQFE class Person def say_hi name = "Peter" end

    def whats_my_name name end end Friday, March 8, 13
  20. 7BSJBCMFT BSFMPDBMTDPQFE >> person = Person.new => #<Person:0x007feb9613b8c8> >> person.say_hi

    => "Peter" >> person.whats_my_name NameError: undefined local variable or method `name' for #<Person:0x007feb9613b8c8> ! from (irb):27:in `whats_my_name' ! ... Friday, March 8, 13
  21. *OTUBODF7BSJBCMF class Person def say_hi @name = "Peter" end def

    whats_my_name @name end end Friday, March 8, 13
  22. *OTUBODF7BSJBCMFTBSFDMBTT TDPQFE >> person = Person.new => #<Person:0x007feb9613b8c8> >> person.say_hi

    => "Peter" >> person.whats_my_name => “Peter” Friday, March 8, 13
  23. 1VCMJD"DDFTTJCJMJUZ class Person def initialize @name = "Peter" end end

    Friday, March 8, 13
  24. 1VCMJD"DDFTTJCJMJUZ >> person = Person.new => #<Person:0x007feb962e6a10> >> person.name NoMethodError:

    undefined method `name' for #<Person: 0x007feb962e6a10> ! from (irb):52 Friday, March 8, 13
  25. BUUS@SFBEFS class Person attr_reader :name def initialize @name = "Peter"

    end end Friday, March 8, 13
  26. 1VCMJD"DDFTTJCJMJUZ >> person = Person.new => #<Person:0x007feb962e6a10> >> person.name =>

    “Peter” Friday, March 8, 13
  27. BUUS@BDDFTTPS class Person attr_accessor :name end >> person = Person.new

    => #<Person:0x007feb96346dc0> >> person.name = "Pedro" => "Pedro" Friday, March 8, 13
  28. .PEVMFT module Nice def when_person_says_hello "hi! how are you?" end

    end class Person include Nice end Friday, March 8, 13
  29. .PEVMFT >> Person.new.when_person_says_hello => “hi how are you?” Friday, March

    8, 13
  30. *OIFSJUBODF class Person attr_reader :name def initialize @name = "Peter"

    end end class Pedro < Person end Friday, March 8, 13
  31. *OIFSJUBODF >> pedro = Pedro.new => #<Pedro:0x007feb96db9780 @name="Peter"> >> pedro.name

    => "Peter" >> pedro.is_a? Person => true Friday, March 8, 13
  32. &OVNFSBCMFT Friday, March 8, 13

  33. DPMMFDUJPOTPS BSSBZT Friday, March 8, 13

  34. -PPQJOH 3.times do puts "hi" end hi hi hi =>

    3 Friday, March 8, 13
  35. -PPQJOH [1, 2, 3].each do |i| puts i * 25

    end 25 50 75 => [1, 2, 3] Friday, March 8, 13
  36. .BQ [1, 2, 3].map do |i| i * 25 end

    [1, 2, 3].map{ |i| i * 25 } => [25, 50, 75] Friday, March 8, 13
  37. *OKFDU [5, 10, 15].inject do |memo, i| memo + i

    end [5, 10, 15].inject(:+) => 30 Friday, March 8, 13
  38. *OKFDU8PX&YBNQMF %w{ cat sheep bear }.inject do |memo, word| memo.length

    > word.length ? memo : word end => "sheep" Friday, March 8, 13
  39. 1VUJU"MM5PHFUIFS Account.all.reject{|i| i.active?}.map{|a| a.amount_due}.inject(:+) Account .all .reject{|a| a.active?} .map{|a| a.amount_due}

    .inject(:+) Friday, March 8, 13
  40. [PNH Friday, March 8, 13

  41. )BTIFT Friday, March 8, 13

  42. )BTI.BQPS %JDUJPOBSZ Friday, March 8, 13

  43. )BTI4ZOUBY our_hash = Hash.new our_hash["location"] = "Houston" our_hash["location"] => "Houston"

    our_hash = {} our_hash["location"] = "Houston" our_hash.fetch("location") => "Houston" Friday, March 8, 13
  44. )BTIXIFONJTTJOH our_hash = Hash.new our_hash["location"] = "Houston" our_hash["area"] => nil

    our_hash = {} our_hash["location"] = "Houston" our_hash.fetch("area") KeyError: key not found: "area" ! from (irb):300:in `fetch' ! from (irb):300 Friday, March 8, 13
  45. )BTI%FGBVMUT our_hash = Hash.new our_hash["location"] = "Houston" our_hash["area"] || "Texas"

    => “Texas” our_hash = {} our_hash["location"] = "Houston" our_hash.fetch("area", "Texas") => “Texas” Friday, March 8, 13
  46. &YUSB"SHVNFOUTBSF )BTIFE def gimme_gimme(name, args) puts "location: #{args[:location]}" puts "location:

    #{args[:area]}" end gimme_gimme "roy", location: "nyc", area: "NY" location: nyc location: NY Friday, March 8, 13
  47. UZQFTPG4USJOHT lSFHVMBSTUSJOH\bIJ`^z lSFHVMBSTUSJOHIJz bOPOJOUFSQSFUFETUSJOH\OBNF^` bOPOJOUFSQSFUFETUSJOH\OBNF^` TZNCPM Friday, March 8, 13

  48. #MPDLTBOE 1SPDT Friday, March 8, 13

  49. 5JNJOH&YBNQMF def how_long? start_time = Time.now result = yield end_time

    = Time.now [result, end_time - start_time] end how_long? do sleep(2) "pretend call to an api" end Friday, March 8, 13
  50. 5JNJOH&YBNQMF => ["pretend call to an api", 2.00012] Friday, March

    8, 13
  51. -PHHJOH&YBNQMF class EasyLogger def initialize @log = [] end def

    log(description) @log << "#{description} - #{Time.now}" yield end def entries @log end end logger = EasyLogger.new %w(sarah bobby jungle stuff).map do |word| logger.log "reverse #{word}" do word.reverse end end Friday, March 8, 13
  52. -PHHJOH&YBNQMF => ["haras", "ybbob", "elgnuj", "ffuts"] >> logger.entries ["reverse sarah

    - 2013-03-07 11:47:25 -0600", "reverse bobby - 2013-03-07 11:47:25 -0600", "reverse jungle - 2013-03-07 11:47:25 -0600", "reverse stuff - 2013-03-07 11:47:25 -0600"] http://rubyfiddle.com/riddles/10504/9 Friday, March 8, 13
  53. &WFSZ.FUIPEBTB#MPDL PQUJPOBMMZ def name(v1, *other_args, &the_block) Friday, March 8, 13

  54. #MPDLJTJHOPSFEJGOPU ZJFMEFEUP def x_plus_y(x, y) x+y end x_plus_y( "a", "b")

    do puts "I was called!" end => "ab" Friday, March 8, 13
  55. #MPDLJTJHOPSFEJGOPU ZJFMEFEUP def x_plus_y(x, y) yield x+y end x_plus_y( "a",

    "b") do puts "I was called!" end I was called! => "ab" Friday, March 8, 13
  56. B1SPDJTBDBMMBCMFPCKFDU my_callback = Proc.new { puts Time.now } def show_off(callable)

    puts Time.now sleep(2) callable.call end show_off my_callback Friday, March 8, 13
  57. B1SPDJTBDBMMBCMFPCKFDU my_callback = Proc.new { puts Time.now } def show_off(callable)

    puts Time.now sleep(2) callable.call end show_off my_callback 2013-03-07 15:56:48 -0600 2013-03-07 15:56:50 -0600 Friday, March 8, 13
  58. B1SPDJTBDBMMBCMFPCKFDU my_callback = Proc.new { puts Time.now } def show_off(callable)

    puts Time.now sleep(2) callable.call end show_off my_callback 2013-03-07 15:56:48 -0600 2013-03-07 15:56:50 -0600 Friday, March 8, 13
  59. (PPEVTF$BMMCBDLT yay = Proc.new {|report| DPS.submit! report } boo =

    Proc.new {|report| FailureLog.log report } methods = {success: yay, failure: boo} class Processor def process(report, callbacks={}) if report.valid? callbacks.fetch(:success, Proc.new).call(report) else callbacks.fetch(:failure, Proc.new).call(report) end end end Processor.process(report, methods) Friday, March 8, 13
  60. BQQSPWF Friday, March 8, 13

  61. .FUB 1SPHSBNNJOH Friday, March 8, 13

  62. 0QFOJOH"OZ$MBTT &WFS class Fixnum def is_answer_to_everything? self == 42 end

    end 5.is_answer_to_everything? Friday, March 8, 13
  63. 0QFOJOH"OZ$MBTT &WFS class Fixnum def is_answer_to_everything? self == 42 end

    end 5.is_answer_to_everything? => false Friday, March 8, 13
  64. #VUXIBUJTUIFBOTXFS (1..100) .find(&:is_answer_to_everything?) => 42 Friday, March 8, 13

  65. #VUXIBUJTUIFBOTXFS class Fixnum def is_answer_to_everything? self == 42 end end

    (1..Float::INFINITY).find(&:is_answer _to_everything?) => 42 # Note it did not error! Friday, March 8, 13
  66. #FJOH&WJM class Fixnum def ==(other) true end end 5 ==

    5 => true Friday, March 8, 13
  67. #FJOH&WJM class Fixnum def ==(other) true end end 5 ==

    5 => true 6 == 62 => true Friday, March 8, 13
  68. *OTUBODF&WBM 42.instance_eval do def is_answer_to_everything? self == 42 end end

    4.is_answer_to_everything? NoMethodError: undefined method `is_answer_to_everything?' for 4:Fixnum 42.is_answer_to_everything? => true Friday, March 8, 13
  69. &YUFOE0CKFDUTXJUI.PEVMFT user = Object.new admin = Object.new module Purchaseable def

    purchase! # Order.create! end end admin.extend Purchaseable user.purchase! admin.purchase! Friday, March 8, 13
  70. &YUFOE0CKFDUTXJUI.PEVMFT user.purchase! NoMethodError: undefined method `purchase!' admin.purchase! => nil #

    it works! Friday, March 8, 13
  71. .FUIPE.JTTJOH class OhHai def method_missing(name, *args, &block) if name.to_s =~

    /^say_/ name.to_s.gsub('say_', '').upcase else super end end end Friday, March 8, 13
  72. .FUIPE.JTTJOH class OhHai def method_missing(name, *args, &block) if name.to_s =~

    /^say_/ name.to_s.gsub('say_', '').upcase else super end end end OhHai.new.say_hello Friday, March 8, 13
  73. .FUIPE.JTTJOH class OhHai def method_missing(name, *args, &block) if name.to_s =~

    /^say_/ name.to_s.gsub('say_', '').upcase else super end end end => "HELLO" OhHai.new.say_hello Friday, March 8, 13
  74. .FUIPE.JTTJOH class OhHai def method_missing(name, *args, &block) if name.to_s =~

    /^say_/ name.to_s.gsub('say_', '').upcase else super end end end => "HELLO" OhHai.new.say_wasssup OhHai.new.say_hello Friday, March 8, 13
  75. .FUIPE.JTTJOH class OhHai def method_missing(name, *args, &block) if name.to_s =~

    /^say_/ name.to_s.gsub('say_', '').upcase else super end end end => "HELLO" OhHai.new.say_wasssup OhHai.new.say_hello => "WASSSUP" Friday, March 8, 13
  76. *OEFFE Friday, March 8, 13

  77. "DUJWF4VQQPSU $PPM4UVGG Friday, March 8, 13

  78. 6UJMJUZDMBTTFTBOE 3VCZFYUFOTJPOT GSPN3BJMT Friday, March 8, 13

  79. $PPM4UVGG require 'active_support/all' 1.day.from_now => 2013-03-08 13:07:28 -0600 3.weeks.ago =>

    2013-02-14 13:08:27 -0600 (1..50).to_a.forty_two => 42 Friday, March 8, 13
  80. <NPSF>$PPM4UVGG require 'active_support/all' "2012-03-31".to_date => Sat, 31 Mar 2012 {first:

    "Jesse", last: "Wolgamott"}.to_json => {"first":"Jesse","last":"Wolgamott"} # and much much more Friday, March 8, 13
  81. 5FTUJOH Friday, March 8, 13

  82. 8IBUXFXBOUUPUFTU class CloneWar def war? false end end Friday, March

    8, 13
  83. .JOJ5FTU require 'minitest/autorun' class TestCloneWar < MiniTest::Unit::TestCase def test_should_not_be_at_war assert_equal

    false, CloneWar.new.war? end end Friday, March 8, 13
  84. 34QFD require 'rspec' describe CloneWar do it "should not be

    at war" do subject.war? should be_false end end Friday, March 8, 13
  85. FJUIFSJTHSFBU Friday, March 8, 13

  86. &DPTZUFN Friday, March 8, 13

  87. 5IF3VCZ&DPTZTUFN 3BJMTUIFCJHEBEEZ (FNTSFVTBCMFMJCSBSJFT #VOEMFSJOTUBMMTHFNTEFQFOEFODJFT Friday, March 8, 13

  88. 4UBDL Friday, March 8, 13

  89. (FOFSBMMZ1SFGFSSFE4UBDL 1PTUHSF42-GPSEC NFNDBDIFEBOESFEJTUPIFMQ OHJOYGPSXFCTFSWFS VOJDPSOPSQVNBGPSSBJMTTFSWFS IFSPLVGPSIPTUJOH HJUGPSEFQMPZ PS DBQJTUSBOPUPEFQMPZ Friday,

    March 8, 13
  90. <pO> *`N+FTTF8PMHBNPUU *DSFBUFBOEUSBJOBU$PNBM1SPEVDUJPOT *SVO3VCZ0GG3BJMT Friday, March 8, 13