Monday, March 30, 2009

Workshop 4: Riding the Rails with Ruby

To do:
1. Spend some time moving your way through the 46 Ruby coding examples in the Ruby Tutorial with Code from http://www.fincher.org/tips/Languages/Ruby/

I have go through the 46 Ruby coding examples:
  1. Install Ruby
  2. Our first program
  3. Output in Ruby
  4. Reading from the Console
  5. Functions
  6. Open Classes
  7. Variable naming
  8. Interesting tidbits about Ruby
  9. Variable Types
  10. Missing
  11. Quotes
  12. Objects
  13. Big Numbers
  14. Parallel Assignment
  15. Collections (Arrays, Hashes, Ranges)
  16. Control Statements (if, case, for, exit, loop)
  17. Statement modifiers (if, unless, while)
  18. Iterators (while, times, each, each with ranges, upto)
  19. You gotta have class (classes, ToString, to_s, subclassing, virtual attributes)
  20. Regular Expressions
  21. Blocks
  22. File I/O
  23. method_missing - a wonderful idea
  24. "BEGIN" / "END"
  25. converting between strings and ints
  26. Using XML Dom Parser
  27. Run a few lines directly from the command line with the "-e" option
  28. Editing files in place
  29. Example of printing duplicate lines in sorted file
  30. Ruby has it own interpreted shell, irb
  31. Ruby can take input stdin
  32. to pass a string on the uri it needs to "escape" first
  33. Example to remove "funny" characters from a filename
  34. Looping over list of arguments
  35. Miscellaneous Commands
  36. DataTime
  37. Using 'requie'
  38. BuiltIn Command Interpreter
  39. Introspection with ObjectSpace
  40. Testing
  41. Read a URL and print the web page to the screen
  42. Example of drawing a line on a canvas in Tk
  43. irb - interactive ruby
  44. RubyGems a ruby package installer
  45. Ruby on Rails
  46. Ruby Quotes
2. What are the syntax differences in the way that Ruby and Javascript use the if statement?

Ruby if statement:


Javascript if statement:


The syntax differences in Ruby and Javascript use the if statement:
  1. Both Ruby and Javascript If statement start with " if "
  2. Both of them write the actual condition just after the word " if ", but Javascript write the actual condition in between the open " ( " and closed " ) " brackets
  3. In Ruby specifies what will happen if the condition is satisfied, is written before word " end ", but in Javascript it is written between open " { " and closed " } " curly brackets (or braces)
  4. In Ruby uses the " else " or " elseif ", but in Javascript uses " else " or " else if " to specify what to do next if the condition is not satisfied.
3. While Ruby and Python are quite similar, can you find some similarities between Ruby and Javascript?

Ruby has many of the same language features that JavaScript has, including (Seidman, 2006):
  • extension by mixin (mixin in OO programming, is a class that provides a certin functionality to by inherited by a subclass)
  • duck typing (duck typing is a style of dynamic typing in which an object's current set of methods and properties determines the vaild semantics, rather than its inheritance from a particular class or implementation of a spceific interface.)
  • a YAML equivalent JavaScript Object Notation (YAML is a human-readable data serialization format that takes concepts from language sech as XML, C, Python, Perl)
  • arbitrary class and object extension (ie. adding methods/properties to classes or specific objects at any time)
  • simple arrays and hashes
  • has first-class functions, which are the same (though not identical) as blocks.

Challenge Problems:
1. Create, test and debug a Ruby program called dognames.rb or catnames.rb to accept 3 names from the keyboard and to display each name on the screen in alphabetical order WITHOUT using a data structure such as a list.

The dognames.rb program:


The testing of the dognames.rb program:


2. Write a Ruby program called fizzbuzz.rb that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

The fizzbuzz.rb program:


The result of the fizzbuzz.rb program:


3. Compare the Ruby and Python versions of the dog years calculator:


Reference
  1. Seidman, G. (2006), Faq and javascript vs. ruby (was re: rubynuby - client side ruby?), newgroups.derkeller.com, Retrieved from http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.ruby/2006-02/msg02949.html on 1st April, 2009

No comments:

Post a Comment