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

9 Things Every Ruby Newbie Should Know

9 Things Every Ruby Newbie Should Know

Brandon Keepers

January 02, 2012
Tweet

More Decks by Brandon Keepers

Other Decks in Programming

Transcript

  1. Jim Weirich 10 Things Every Java Programmer Should Know About

    Ruby http://onestepback.org/articles/10things/
  2. linguistic relativity the structure of a language affects the ways

    in which its speakers are able to conceptualize their world
  3. “A language that doesn’t affect the way you think about

    programming is not worth knowing.” Alan Perlis
  4. no primitives 0.zero? # => true 1.zero? # => false

    -1.abs # => 1 10.class # => Fixnum (10**100).class # => Bignum true.class # => TrueClass
  5. nil is an object a = nil a.nil? # =>

    true a.methods # => list of methods a.class # => NilClass a.abs # => NoMethodError
  6. computation… 1. assignment (binding objects to names) 2. primitive control

    structures (if, else, while) and operators (defined?) 3. sending messages to objects
  7. method_missing class VCR def initialize @messages = [] end def

    method_missing(method, *args, &block) @messages << [method, args, block] end def play_back_to(obj) @messages.each do |method, args, block| obj.send(method, *args, &block) end end end
  8. method_missing require 'vcr' vcr = VCR.new vcr.sub!(/Hello/) { "Goodbye" }

    vcr.upcase! vcr[8,5] = "Universe" vcr << "!" string = "Hello World" puts string vcr.play_back_to(string) puts string Outputs: Hello World GOODBYE Universe!
  9. static String str = new String("compile"); str = 5; incompatible

    types found : int required: java.lang.String
  10. duck typing class Duck def talk "quack!" end end class

    Goose def talk "honk!" end end birds = [Duck.new, Goose.new] birds.each {|b| b.talk }
  11. mix-in module Logging def logger @logger ||= Logger.new(opts) end end

    class Person include Logging def relocate logger.debug "Relocating person..." # do relocation stuff end end
  12. Comparable class SizeMatters < Struct.new(:str) include Comparable def <=>(other) str.size

    <=> other.str.size end end s1 = SizeMatters.new("Z") s2 = SizeMatters.new("YY") s3 = SizeMatters.new("XXX") s1 < s2 #=> true s3.between?(s1, s2) #=> false s2.between?(s1, s3) #=> true [ s3, s1, s2 ].sort #=> [Z, YY, XXX]
  13. optional parenthesis puts 'Hello World' puts('Hello World') 'Hello World'.count 'l'

    puts 'Hello World'.count 'l' puts 'Hello World'.count('l')
  14. open classes class Integer def even? (self % 2) ==

    0 end end (1..10).select { |n| n.even? } # => [2, 4, 6, 8, 10]
  15. Summary 1. learn the Ruby way 2. follow the conventions

    3. all values are objects 4. everything is a message 5. truthiness 6. strong dynamic typing 7. modules 8. embrace enclosures 9. Ruby is loose
  16. License This presentation is based on “10 Things Every Java

    Programmer Should Know About Ruby” by Jim Weirich. http://onestepback.org/articles/10things/license.html This presentation is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License. http://creativecommons.org/licenses/by-nc/3.0/