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

20bfe76b3d6105641f879fe45cfc9272?s=128

Brandon Keepers
PRO

January 02, 2012
Tweet

Transcript

  1. 9 @bkeepers things every Ruby Newbie should know

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

    Ruby http://onestepback.org/articles/10things/
  3. learn the Ruby way

  4. linguistic relativity the structure of a language affects the ways

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

    programming is not worth knowing.” Alan Perlis
  6. don’t just write in Ruby Java

  7. don’t just write in Ruby Perl

  8. don’t just write in Ruby PHP

  9. don’t just write in Ruby C#

  10. Learn the Ruby Way

  11. follow the conventions

  12. ClassName

  13. @instance_variable

  14. $global_variable

  15. SomeConstant ANOTHER_CONSTANT

  16. method_name variable_name

  17. method?

  18. dangerous_method!

  19. ClassName file_name.rb

  20. Class#method

  21. 2 space indent

  22. some are enforced

  23. some are community standards

  24. all values are objects

  25. anything that can be bound to a variable

  26. no primitives 0.zero? # => true 1.zero? # => false

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

    true a.methods # => list of methods a.class # => NilClass a.abs # => NoMethodError
  28. classes are objects a = Array.new

  29. almost everything is a message

  30. computation… 1. assignment (binding objects to names) 2. primitive control

    structures (if, else, while) and operators (defined?) 3. sending messages to objects
  31. messages are not just function calls

  32. function calls use internal lookup tables

  33. messages delegate to the object

  34. 'hello'.count('l')

  35. 'hello'.send(:count, 'l')

  36. operators are syntactic sugar 1 + 2 1.+(2) 1.send(:+, 2)

  37. array[0] array.send(:[], 0)

  38. 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
  39. 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!
  40. truthiness

  41. nil and false are false

  42. everything else is true

  43. puts "including zero" if 0

  44. Strong dynamic typing

  45. weak <?php echo 5 + "5"; ?>

  46. strong 5 + "5" TypeError: String can't be coerced into

    Fixnum
  47. 5 + "5".to_i

  48. strong dynamic typing

  49. static String str = new String("compile"); str = 5; incompatible

    types found : int required: java.lang.String
  50. dynamic a = "runtime" a = [ ]

  51. strong dynamic typing

  52. 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 }
  53. modules

  54. namespace module GitHub class User end end user = GitHub::User.new

  55. 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
  56. 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]
  57. embrace closures

  58. Iteration [1,2,3].each { |item| puts item }

  59. Resource Management file_contents = open(file_name) { |f| f.read }

  60. Callbacks widget.on_button_press { puts "Got Button Press" }

  61. Ruby is loose

  62. optional parenthesis puts 'Hello World' puts('Hello World') 'Hello World'.count 'l'

    puts 'Hello World'.count 'l' puts 'Hello World'.count('l')
  63. optional semicolons puts 'Hello World'; don’t use them

  64. “constants” Timeout = 30 Timeout = 50 warning: already initialized

    constant Timeout puts Timeout 50
  65. open classes class Integer def even? (self % 2) ==

    0 end end (1..10).select { |n| n.even? } # => [2, 4, 6, 8, 10]
  66. 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
  67. Thanks! @bkeepers

  68. 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/