The document provides an overview of what the author loves about Ruby, summarizing several key features in 3 sentences or less:
Ruby allows for concise and clear code with less ceremony than other languages like Java. It features an expressive syntax, powerful enumerable processing methods, and the ability to pass code blocks to functions as arguments. Ruby treats everything as an object, including primitive types like integers, and includes features like ranges, regular expressions, and metaprogramming capabilities.
28. Conciseness and Clarity, Not Ceremony: Multiline Strings """ Dear #{customer_name}, Thank you for your purchase on #{formatAsDate(purchase_date)}. We have billed your credit card for #{formatAsMoney(purchase_amount)}. """
39. Inside the function, block_given? Can be called to determine the presence/absence of the block. irb(main):001:0> def foo irb(main):002:1> if block_given? ; yield; else; puts "No block given."; end irb(main):003:1> end irb(main):005:0* foo { puts "I'm passing a block." } I'm passing a block. irb(main):006:0> foo No block given.
40.
41. Automatic Cleanup or State Restoration Using Closures File.open('myfile.txt') do |file| file << 'hi' # do something with the file end # the file is automatically closed Dir.chdir('another/dir') do # do something in that directory end # chdir's back to the original directory
42. Everything’s an Object, Even 1 and nil irb(main):020:0> 1.class => Fixnum irb(main):021:0> [].class => Array irb(main):022:0> {}.class => Hash irb(main):023:0> //.class => Regexp irb(main):024:0> self.class => Object irb(main):025:0> nil.class => NilClass irb(main):001:0> (0..10).class => Range
50. Regular Expression Position Using =~ irb(main):007:0> 'peach' =~ /c/ => 3 irb(main):008:0> 'mango' =~ /c/ => nil
51. Regular Expressions in Case Statements irb(main):013:0> def test_for_a(word) irb(main):014:1> case word irb(main):015:2> when /^a/ irb(main):016:2> puts "#{word} begins with an 'a'." irb(main):017:2> else irb(main):018:2* puts "#{word} does *not* begin with an 'a'." irb(main):019:2> end irb(main):020:1> end irb(main):021:0> irb(main):022:0* test_for_a('mango') mango does *not* begin with an 'a'. irb(main):023:0> test_for_a('apple') apple begins with an 'a'.