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/
Answer:
I have already studied the Ruby Tutorial from the mentioned URL.
2. what are the syntax differences in the way that Ruby and Javascript use the if statement?
Answer:
The if-elseif-else example of ruby is listed below...
temperature=30
if temperature <=15
puts "It is very cold"
elsif temperature>15 && temperature<=25
puts "it is warm"
else
put "it is hot"
end
The if-elseif-else example of Javascript is listed below...
var temperature=30;
if (temperature<=15)
{ document.write("It is very cold"); }
else if (temperature>15 && temperature<=25)
{ document.write("It is warm"); }
else
{ document.write("It is hot"); }
With the above 2 examples, the syntax difference between Ruby and Javascript is explained here...
- In Ruby, we use "if condition". However, in Javascript, we use "if (condition)". The difference is the "( )"
- The else-if statement in Ruby is performed by reserved words "elsif", but Javascript use "else if".
- Similarly, "elsif condition" in ruby is different with "else if (condition)" in Javascript. The difference is the "( )"
- Ruby needs "end" to complete the if-elsif-else logic, yet Javascript does not need "end" because Javascript executes statements as block "{ }"
- In Javascript, if the condition is true, the statement(s) inside "{ }" will be executed. The logic flow is that Javascript first checks the if-clause to see whether the condition is true and then it checks the else-if-clause. If the if-clause and else-if-clause' conditions are false, then Javascript will run else' statement(s) inside "{ }". In Ruby, if-elseif-else logic flow is that Ruby first checks the if-clause to verify whether the condition is true. If true, Ruby executes the statement(s) after the if-clause. The execution stops until reaching the elsif or else clauses and jumps to "end" statement. If the first if-clause's condition is false, then Ruby jumps to execute elsif's statements until reaching else......
3. While Ruby and Python are quite similar, can you find some similarities between Ruby and Javascript?
Answer:
Both languages are highly dynamic, allowing you to change objects and methods at runtime. Both languages are very object-oriented with different approaches. Ruby and JavaScript are both dynamically typed and often employ what is known in the Ruby world as duck typing instead of checking for certain types. 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. This is similar to Ruby’s notion of references and immediate values with the only difference that the latter group are not real objects in JavaScript. JavaScript, like Ruby, is heavily object-oriented, and objects are used everywhere. In JavaScript and Ruby, the only construct which provides scope is the function. In Ruby, classes are “open” and you can extend any class with new methods. This is also the case for JavaScript. (Darell, 2009)
Reference:
1. Darell Tore (2009). "JavaScript Eye For Ruby Guy". Sneaky Abstractions, Retrieved from URL - http://tore.darell.no/pages/javascript_eye_for_the_ruby_guy
No comments:
Post a Comment