Codecademy Practice
Comparison: Ruby vs JavaScript
-
Ruby has no
var,letorconst. You just type the name of the variable and if you want it to be a constant you type it in uppercase. -
Ruby uses no parenthesis for functions, unless the function takes arguments, in which case they are optional.
-
Ruby uses no curly brackets
{}to contain the body of a function, it uses the delimitersdefandend -
if you can’t access something, it just returns null, it doesn’t error
-
you can not pass functions as arguments as you do in JavaScript
-
private stuff is actually really 100% private for reals
Ruby has:
-
no semicolons at the end of every line
; -
no
thiskeyword -
no return, it returns whatever is in the last line (
returnexists but it used to exit early)
Ruby specific features:
-
nullis callednil -
lengthis also calledsizeandcount, so you can use any of them with the same result -
three equals (
x === y) is just two equals (x == y) -
String concatenation is done using double quotes and adding all code inside this brackets:
#{} -
In Ruby you can not only do something
ifsome condition happens, you can also do somethingunlesssome condition happens -
You can also loop
whilesome condition is met oruntilsome condition is met. -
getters are called “attribute readers”
-
setters are called “attribute writers”
-
setter + getter is called “attribute accessor”
-
boolean methods have to end in question mark
? -
mutation methods end in exclamation mark
!. These methods change the object they act on in place, rather than returning a new object.