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:
- Install Ruby
- Our first program
- Output in Ruby
- Reading from the Console
- Functions
- Open Classes
- Variable naming
- Interesting tidbits about Ruby
- Variable Types
- Missing
- Quotes
- Objects
- Big Numbers
- Parallel Assignment
- Collections (Arrays, Hashes, Ranges)
- Control Statements (if, case, for, exit, loop)
- Statement modifiers (if, unless, while)
- Iterators (while, times, each, each with ranges, upto)
- You gotta have class (classes, ToString, to_s, subclassing, virtual attributes)
- Regular Expressions
- Blocks
- File I/O
- method_missing - a wonderful idea
- "BEGIN" / "END"
- converting between strings and ints
- Using XML Dom Parser
- Run a few lines directly from the command line with the "-e" option
- Editing files in place
- Example of printing duplicate lines in sorted file
- Ruby has it own interpreted shell, irb
- Ruby can take input stdin
- to pass a string on the uri it needs to "escape" first
- Example to remove "funny" characters from a filename
- Looping over list of arguments
- Miscellaneous Commands
- DataTime
- Using 'requie'
- BuiltIn Command Interpreter
- Introspection with ObjectSpace
- Testing
- Read a URL and print the web page to the screen
- Example of drawing a line on a canvas in Tk
- irb - interactive ruby
- RubyGems a ruby package installer
- Ruby on Rails
- Ruby Quotes
Ruby if statement:

Javascript if statement:

The syntax differences in Ruby and Javascript use the if statement:
- Both Ruby and Javascript If statement start with " if "
- 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
- 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)
- 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.
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
- 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