Monday, April 6, 2009

Workshop 4

TO Do List for Workshop 4

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/

On this tutorial, you can learn basic to advanced programming techniques on ruby. Besides that, you will learn quite efficient tips on ruby programming language.

This tutorial covers following important topics;

1. Getting input from user
2. Learning how is a function called by another function
3. Basic syntax structure and default functions in ruby such as, upcase, puts...etc.
4. Adding methods to existing library classes.
5. Variable naming. For example meanings of $, @@, @, ?, !, #, \, __END__, and so on...
6. Object oriented programming techniques
7. Data structures
8. Control statements, such as if, when, while...
9. Regular expressions which are surrounded by "//" or "%r{}".
10. File input/output methods
11. Miscellanous Commands
12. Using of REPL (Read, Eval, Print, Loop)
13. Ruby on Rails properties
14. Watir GUI usage
15. How to use Watir with NUnit

After this tutorial, we can say that ruby is a fourth generation language like JAVA. It provides many easy ways to develop an application.

2) What are the syntax differences in the way that Ruby and Javascript use the if
statement?

If statement synatx for Javascript;

if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if condition1 and
condition2 are not true
}


If statement syntax for Ruby

if condition1
code to be executed if condition1 is true

elseif condition2
code to be executed if condition2 is true

else
code to be executed if condition1 and
condition2 are not true

end

As it can be seen from above descriptions, Ruby is a very clean and readable programming language. Unlike Javascript, it is not needed with curly brackets and space between else and if key words or hard to understand method names. But in order to specify end of the if statement block, you need to write "end" under the last condition in Ruby.


3) While Ruby and Python are quite similar, can you find some similarities between Ruby and Javascript?

1. Perl and Python provide unlimited extent for variables captured in closures, but Ruby and Javascript do not.
2. Both of them are object oriented languages. So, they need same programming knowledge.
3. In javascript, any function can be passed as a parameter like in ruby.
4. In both languages, an object without functions can use another object's functions if they are exist. (Scott, 2006, p. 724)

References

Scott, M.L. (2006). Programming language pragmatics (2nd ed.). Morgan Kaufmann Press



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.

You can see catnames.rb code and its' executed result below windows;






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


You can see fizzbuzz.rb code and its' executed result below windows;








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

#!/usr/bin/ruby
# The Dog year calculator program called dogyears.rb

def dogyears
# get the original age
puts “Enter your age (in human years): "
age = gets # gets is a method for input from keyboard
puts # is a method or operator for screen output

#do some range checking, then print result
if age <> 110
puts "Frankly, I don't believe you."
else
puts "That's", age*7, "in dog years."
end
dogyears

Python

#!/usr/bin/python
# The Dog year calculator program called dogyears.py

def dogyears():
# get the original age
age = input("Enter your age (in human years): ")
print # print a blank line

# do some range checking, then print result
if age <> 110:
print "Frankly, I don't believe you."
else:
print "That's", age*7, "in dog years."

### pause for Return key (so window doesn't disappear)
raw_input('press Return>')

def main():
dogyears()
main()




1. In Ruby, constants begin with a capital letter, local variables with a small letter, global variables with the prefix $ and instance variables, attributes with the prefix @.
2. Method definition styles differ from each other. Ruby does not uses bracklets but Python does.
3. Read and write commands also differ from each other.
4. In Ruby, there is a "pause command" but Python does not use.
5. Ruby's condition statements are different from Python's condition statements. For example, in ruby; "elseif" and "end" key words are used. In Python, just uses "elif" key word.

No comments:

Post a Comment