Day 29, Ruby

This is the fifth week. Scary!

Most students were unsatisfied with the database sprint setup and we spent a hour on the sprint reflection. I like the fact that Marcus and other stuff members took those feedbacks very seriously.

Primer on algorithms:

Two constrains are execution time and relative memory allocation; three major tasks are store data, sort data and search data. As the hiring day is approaching for us, I would spend significant more time on this topic.


The principle of least surprise(?)

Good parts:

– tons of built-in functions; some very nice datatypes such as date or currency

– Rails is probably the reason why Ruby is so popular

Other parts:

– Server side only

Compared to JS:

– Ruby has block level scope

for obj.keys do |key|

#block scope


While in JS, block scope doesn’t exist, hence the necessity to wrap blocks with function(){}. It makes sense since most people want to run the same code block with some arguments, we might as well use function instead.

var f1 = function(){

//function scope


– in hash or object, the key can be other types beyond string. Instead of using key:value, the notation is hash rocket, meaning key=>value.

– callbacks

Ruby. Thought process: since we rarely pass more than one callback function(block), let’s always give the function a block that can be used as a callback; in that case, we might as well give the function a default name: yield. (Note: this is not 100% accurate. )

def f1

f1 do |args|

  puts args



var f1 = function(callback){
var callback = function(args){

– collection iterations. Ruby has its built-in each method while JavaScript has to rely on underscore.js.

[1,2,3].each do |ele|
puts ele



– class
Ruby has the class keyword while JavaScript doesn’t.

class Person
  @@n_hands = 2 # class level variable
  attr_accessor :age
  def initialize age
    @age = age
a =
p a.age
p Person.class_variable_get(:@@n_hands)

Same code in JavaScript:

var Person = function(age){
  this.age = age;
Person.n_hands = 2;
var a = new Person(30);

– private or public properties/methods of objects
Ruby only exposes methods, meaning all properties are private. For examples, obj.keys means obj.keys(). To get/set properties, one has to generate getter/setter by themselves. There is a shortcut to do so using “attr_accessor :some_var”. According to instructors,

#attr_accessor :some_var
#literally means inserting the following two blocks of code
def some_var
def some_var= input #'some_var=' is the function name, and input is the parameter
  @some_var = input

JavaScript doesn’t care.