0
Who I Am <ul><li>Keith Bennett, keithrbennett at gmail.com
Software Developer living near Washington, DC, USA
Original hometown is New York City
25+ years experience, focusing on Ruby and Java, currently studying Android development
Technical Interests: Ruby, Android, Clojure
Other Interests: Travel, Music, Study of Massage and Asian and European Languages </li></ul>
Obligatory Joke, Part 1 A DBA walks into a bar, steps in front of two tables and says...
Obligatory Joke, Part 2 <ul>...may I join you? </ul>
Please... <ul><li>...ask me to slow down if I speak too quickly.
...ask me again if I forget.
...ask questions if anything I say is not clear.
...feel free to share your own observations and experiences. </li></ul>
What I Love About Ruby: Overview <ul><li>Conciseness and Clarity, Not Ceremony (high signal to noise ratio)
Expressive Syntax
Powerful Enumerable Processing
Code Blocks and Closures
Everything’s an Object, even 1 and nil
Ranges
Regular Expressions
JRuby
IRB
OS Scripting Support
Metaprogramming </li></ul>
Conciseness and Clarity, Not Ceremony: Main Program <ul><li>Java: </li></ul>public class HelloWorld { public static void m...
Conciseness and Clarity, Not Ceremony: Instance Variable Access <ul><li>Java: </li></ul>private String name; public String...
Conciseness and Clarity, Not Ceremony: List/Array Subtraction <ul><li>Java: </li></ul>public static List subtractList(List...
Conciseness and Clarity, Not Ceremony: List Manipulation <ul><li>Java: </li></ul>public static List<Integer> calcDoubles(L...
Upcoming SlideShare
Loading in...5
×

What I Love About Ruby

1,763

Published on

Slides containing information about my favorite features of the Ruby language. Many of these features exist in other languages, but in the context of my experience as a C, C++, and Java developer, offer a dramatic increase in expressiveness and productivity.

This slide show can also be used to teach a Ruby overview.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,763
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
59
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Transcript of "What I Love About Ruby"

  1. 1. Who I Am <ul><li>Keith Bennett, keithrbennett at gmail.com
  2. 2. Software Developer living near Washington, DC, USA
  3. 3. Original hometown is New York City
  4. 4. 25+ years experience, focusing on Ruby and Java, currently studying Android development
  5. 5. Technical Interests: Ruby, Android, Clojure
  6. 6. Other Interests: Travel, Music, Study of Massage and Asian and European Languages </li></ul>
  7. 7. Obligatory Joke, Part 1 A DBA walks into a bar, steps in front of two tables and says...
  8. 8. Obligatory Joke, Part 2 <ul>...may I join you? </ul>
  9. 9. Please... <ul><li>...ask me to slow down if I speak too quickly.
  10. 10. ...ask me again if I forget.
  11. 11. ...ask questions if anything I say is not clear.
  12. 12. ...feel free to share your own observations and experiences. </li></ul>
  13. 13. What I Love About Ruby: Overview <ul><li>Conciseness and Clarity, Not Ceremony (high signal to noise ratio)
  14. 14. Expressive Syntax
  15. 15. Powerful Enumerable Processing
  16. 16. Code Blocks and Closures
  17. 17. Everything’s an Object, even 1 and nil
  18. 18. Ranges
  19. 19. Regular Expressions
  20. 20. JRuby
  21. 21. IRB
  22. 22. OS Scripting Support
  23. 23. Metaprogramming </li></ul>
  24. 24. Conciseness and Clarity, Not Ceremony: Main Program <ul><li>Java: </li></ul>public class HelloWorld { public static void main(String args) { System.out.println(&quot;Hello world!&quot;); } } <ul><li>Ruby: </li></ul>puts “Hello world!”
  25. 25. Conciseness and Clarity, Not Ceremony: Instance Variable Access <ul><li>Java: </li></ul>private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } <ul><li>Ruby: </li></ul>attr_accessor :name
  26. 26. Conciseness and Clarity, Not Ceremony: List/Array Subtraction <ul><li>Java: </li></ul>public static List subtractList(List minuendList, List subtrahendList) { List differenceList = new ArrayList(); for (Object o : minuendList) { if (! subtrahendList.contains(o)) { differenceList.add(o); } } return differenceList; } <ul><li>Ruby </li></ul>minuendList – subtrahendList
  27. 27. Conciseness and Clarity, Not Ceremony: List Manipulation <ul><li>Java: </li></ul>public static List<Integer> calcDoubles(List<Integer> inputList) { List<Integer> outputList = new ArrayList<Integer>(); for (Integer n : inputList) { outputList.add(2 * n); } return outputList; } <ul><li>Ruby: </li></ul>def calc_doubles(input_list) input_list.map { |n| 2 * n } end
  28. 28. Conciseness and Clarity, Not Ceremony: Multiline Strings &quot;&quot;&quot; Dear #{customer_name}, Thank you for your purchase on #{formatAsDate(purchase_date)}. We have billed your credit card for #{formatAsMoney(purchase_amount)}. &quot;&quot;&quot;
  29. 29. Expressive Syntax <ul><li>3.times { puts “I love Ruby! “ }
  30. 30. stooges = [&quot;Moe&quot;, &quot;Larry&quot;, &quot;Curly&quot;] # or
  31. 31. stooges = %w(Moe Larry Curly)
  32. 32. country_codes = { 'Thailand' => 'th', 'Singapore' => 'sg' }
  33. 33. ['mango', 'pineapple', 'durian'][-1] # “durian”
  34. 34. ['mango', 'pineapple', 'durian'].last # “durian”
  35. 35. attr_accessor :id, :name
  36. 36. 1_234_567 # 1234567 </li></ul>
  37. 37. Powerful Enumerable Processing irb(main):001:0> nums = (1..12).to_a => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] irb(main):002:0> squares = nums.map { |n| n * n } => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144] irb(main):003:0> evens = nums.select { |n| n % 2 == 0 } => [2, 4, 6, 8, 10, 12] irb(main):004:0> odds = nums - evens => [1, 3, 5, 7, 9, 11] irb(main):005:0> sum = nums.inject { |sum,n| sum += n } => 78
  38. 38. Code Block Function Parameters <ul><li>Functions can have code blocks passed to them as the last argument without explicitly declaring them.
  39. 39. Inside the function, block_given? Can be called to determine the presence/absence of the block. </li></ul>irb(main):001:0> def foo irb(main):002:1> if block_given? ; yield; else; puts &quot;No block given.&quot;; end irb(main):003:1> end irb(main):005:0* foo { puts &quot;I'm passing a block.&quot; } I'm passing a block. irb(main):006:0> foo No block given.
  40. 40. Closures <ul><li>The puts function is passed the closure (underlined), and has access to the variable name even though name is not in its scope. This makes the code block a closure . </li></ul>irb(main):008:0> name = 'Joe' => &quot;Joe&quot; irb(main):009:0> 3.times { puts &quot;Hi, #{name}! &quot; } Hi, Joe! Hi, Joe! Hi, Joe!
  41. 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. 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
  43. 43. Ranges irb(main):004:0> (0..3).class => Range irb(main):005:0> (0..3).to_a => [0, 1, 2, 3] irb(main):006:0> (0...3).to_a => [0, 1, 2] irb(main):007:0> nums = (0..10).to_a => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] irb(main):008:0> nums[3..5] => [3, 4, 5]
  44. 44. Regular Expressions <ul><li>Can be used as a literal, i.e. without a reference: /^A/
  45. 45. =~ returns an index (or nil if not found).
  46. 46. === returns a boolean value representing the presence or absence of a match.
  47. 47. Can be used in a case statement, and can simulate multimethods in this way. </li></ul>
  48. 48. Regular Expression Literals irb(main):001:0> fruits = %w(apple mango peach pear) => [&quot;apple&quot;, &quot;mango&quot;, &quot;peach&quot;, &quot;pear&quot;] irb(main):002:0> fruits.grep /^p/ => [&quot;peach&quot;, &quot;pear&quot;] irb(main):003:0> p_regex = /^p/; fruits.grep p_regex => [&quot;peach&quot;, &quot;pear&quot;]
  49. 49. Regular Expression Presence/Absence Using === irb(main):005:0> /^p/ === 'peach' => true irb(main):006:0> /^p/ === 'mango' => false
  50. 50. Regular Expression Position Using =~ irb(main):007:0> 'peach' =~ /c/ => 3 irb(main):008:0> 'mango' =~ /c/ => nil
  51. 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 &quot;#{word} begins with an 'a'.&quot; irb(main):017:2> else irb(main):018:2* puts &quot;#{word} does *not* begin with an 'a'.&quot; 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'.
  52. 52. JRuby <ul><li>Runs in the Java Virtual Machine.
  53. 53. Leverages existing Java libraries and infrastructure.
  54. 54. Can be a better Java than Java (e.g. provides irb interactive shell).
  55. 55. Unit testing Java code with JRuby (e.g. with rspec) is more flexible and expressive than testing in JUnit.
  56. 56. JRuby can be used to script DB access via JDBC.
  57. 57. jruby-complete.jar contains Jruby and gem, irb, rake, rdoc, ri utilities. </li></ul>
  58. 58. JRuby as a Java Shell <ul><li>Get available locales: </li></ul>import 'java.util.Locale' Locale.available_locales.each do |locale| puts locale.display_name end <ul><li>Show “os.” System Properties </li></ul>import java.lang.System os_properties = System.properties.select do |key, value| /^os./ === key end
  59. 59. Operating System Scripting <ul><li>Ruby’s power and expressiveness can be combined with OS resources and commands: </li></ul>`find . -name &quot;*.tmp&quot; -exec rm {} &quot;;&quot;` <ul><li>However, Ruby has its own functions that can substitute for OS commands, enabling scripting that's OS-neutral and easier to read: </li></ul>Dir.glob('**/*.tmp').each { |f| File.delete f }
  60. 60. Metaprogramming <ul><li>Enables creation of DSL’s (Domain Specific Languages) such as Ruby on Rails
  61. 61. Enables dramatic simplifications, such as attr_accessor
  62. 62. Enables on-the-fly method creation, such as creating accessor methods on elements in a parsed XML file.
  63. 63. The metaprogrammer’s t-shirt says: “I write code that writes code for food.” </li></ul>
  64. 64. <ul>The End </ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×