Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Jesse Wolgamott

March 07, 2013
Tweet

More Decks by Jesse Wolgamott

Other Decks in Technology

Transcript

  1. $POTUBOUT class MyClass MAXIMUM = 5 def max MAXIMUM end

    end MyClass::MAXIMUM => 5 MyClass.new.max => 5 Friday, March 8, 13
  2. $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
  3. &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
  4. 7BSJBCMFT class Person def say_hi name = "Peter" "hi #{name}"

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

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

    def whats_my_name name end end Friday, March 8, 13
  7. 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
  8. *OTUBODF7BSJBCMF class Person def say_hi @name = "Peter" end def

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

    => "Peter" >> person.whats_my_name => “Peter” Friday, March 8, 13
  10. 1VCMJD"DDFTTJCJMJUZ >> person = Person.new => #<Person:0x007feb962e6a10> >> person.name NoMethodError:

    undefined method `name' for #<Person: 0x007feb962e6a10> ! from (irb):52 Friday, March 8, 13
  11. BUUS@BDDFTTPS class Person attr_accessor :name end >> person = Person.new

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

    end class Person include Nice end Friday, March 8, 13
  13. *OIFSJUBODF class Person attr_reader :name def initialize @name = "Peter"

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

    => "Peter" >> pedro.is_a? Person => true Friday, March 8, 13
  15. -PPQJOH [1, 2, 3].each do |i| puts i * 25

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

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

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

    > word.length ? memo : word end => "sheep" Friday, March 8, 13
  19. )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
  20. )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
  21. )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
  22. &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
  23. 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
  24. -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
  25. -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
  26. #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
  27. #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
  28. 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
  29. 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
  30. 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
  31. (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
  32. 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
  33. #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
  34. #FJOH&WJM class Fixnum def ==(other) true end end 5 ==

    5 => true 6 == 62 => true Friday, March 8, 13
  35. *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
  36. &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
  37. .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
  38. .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
  39. .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
  40. .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
  41. .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
  42. $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
  43. <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
  44. 34QFD require 'rspec' describe CloneWar do it "should not be

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