Monday, September 7, 2009

Workshop 4

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

The JavaScript syntax for if :


if (condition)
{
code to be executed if condition is true
}

The Ruby syntax for if:


if condition
code to be executed if condition is true
end

or alternatively you can write if statements like this:

if (condition) then
code to be executed if condition is true
end

Ruby also allows for extension modifiers, the syntax being:

puts "code to be executed if condition is true" if condition

The JavaScript syntax for if…else:


if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is false
}

The Ruby syntax for if…else:


if condition
code to be executed if condition is true
else
code to be executed if condition is false
end

The JavaScript syntax for if…else if…else:


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 false
}

The Ruby syntax for if…elsif…else:


if condition1
code to be executed if condition1 is true
elsif condition2
code to be executed if condition2 is true
else
code to be executed if condition1 and condition2 are false
end


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

JavaScript has many of the same language features Ruby has, including extension by arbitrary class and object extension (i.e. adding methods/properties to classes or specific objects at any time), simple arrays and hashes...

No comments:

Post a Comment