What I Love About Ruby
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

What I Love About Ruby

on

  • 2,654 views

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, ...

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.

Statistics

Views

Total Views
2,654
Views on SlideShare
2,640
Embed Views
14

Actions

Likes
3
Downloads
58
Comments
0

4 Embeds 14

http://www.iaeronz.com 6
http://www.linkedin.com 6
http://www.lmodules.com 1
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

What I Love About Ruby Presentation Transcript

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