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/
Partially Done!
2. What are the syntax differences in the way that Ruby and Javascript use the if statement?
The syntax of Ruby's If statement which refers to tutorialspoint(2009) at http://www.tutorialspoint.com/ruby/ruby_if_else.htm, that is..
=================
if conditional [then]
code...
[elsif conditional [then]
code...]...
[else code...]
end
=================
"If" expressions are used for conditional execution. The values false and nil are false, and everything else are true. Notice Ruby uses elsif, not else if nor elif.
Executes code if the conditional is true. If the conditional is not true, code specified in the else clause is executed.
An if expression's conditional is separated from code by the reserved word then, a newline, or a semicolon.
For example:
#!/usr/bin/ruby
samAge=20
joeAge=20
if samAge > joeAge
puts "Sam is older than Joe"
elsif samAge < joeAge
puts "Sam is younger than Joe"
else
puts "Sam and Joe are in same age"
end
The syntax of Javascript's If statement which refers to tutorialspoint(2009) at
http://www.tutorialspoint.com/javascript/javascript_ifelse.htm, that is
=============================================
if (expression 1){
Statement(s) to be executed if expression 1 is true
}else if (expression 2){
Statement(s) to be executed if expression 2 is true
}else{ Statement(s) to be executed if no expression is true}
==============================================
It is just a series of if statements, where each if is part of the else clause of the previous statement. Statement(s) are executed based on the true condition, if non of the condition is true then else block is executed.
For example:
From the above "if" statement examples for Ruby and JavaScript, there are some different between them.
Ø An if expression's conditional, Rudy is separated from code by the reserved word then, a newline, or a semicolon, but Javascript needs to use bracket( ) for condition(s) and and use a symbol { } for action(s)
Ø Ruby does not need to declare the data type of variable, but Javascipt needs it.
Ø Ruby uses “elsif”, but Javascript uses “else if”, for handling the other condition between if -else statement.
Ø Ruby uses "puts" and the string needs inside double quote, but Javascript uses document.write(" string here ") for printing out the message.
Ø Ruby need not use semicolon at the end of each statement, but Javascript must require.
3. While Ruby and Python are quite similar, can you find some similarities between Ruby and Javascript?
Darell(2009)(http://tore.darell.no/pages/javascript_eye_for_the_ruby_guy) stated there are some similarities between Ruby and Javascript which are listed in the following:
Ø Both languages are highly dynamic, allowing you to change objects and methods at runtime.
Ø Both languages are very object-oriented, but with different approaches. Ruby is a class-based, object-oriented language with functional aspects while JavaScript is a functional, object-oriented language with some class-like functionality.
Ø Variables in JavaScript, like Ruby, hold data. In the case of basic types, they hold the value of the type itself, but in the case of objects it holds a reference to the object itself.
Ø JavaScript, like Ruby, is heavily object-oriented, and objects are used everywhere.
Ø Functions in JavaScript, like blocks in Ruby, provide closure. That is, the variables available in the scope where the function is defined are always available in the function, even if it’s called from another scope.JavaScript is prototype-based, but it has the concept of classes. Kind of. There is a new keyword which is very familiar to those having used class-based languages. In Ruby this is not a keyword, but a class method, but the concept is the same: It instantiates a new object from that class.
沒有留言:
發佈留言