Day 30, the master says

The master says ” just like a feather, your code is floating in the air. ”

This is from the RubyMonk and we were having a blast going through it. Way way better than the CodeSchool Ruby Intro. When I was going through the CodeSchool Ruby Intro, I was like, “why people love Ruby again?”. When I was going through RubyMonk, now I knows why. Plus, the master has some really cool quotes there.

Some cool functions that I liked when I was going through this Ruby intro sprint: [].map, [].find, [].find_all, [].inject, {}.to_str (makes obj string-like). When I was using Ruby, I kept thinking that Matz must have spent thousands of hours thinking about all those functions. It is day and night compared to JavaScript and I am not sure one way is better than the other.

Lambda vs. Proc vs. Block

Note: just trying to be mostly right here. Totally needs to read more on this.

Blocks are like Procs without visible name. Blocks can be converted to Procs easily.

Lambda errors out with the wrong number of arguments; one of them has power to return differently(?)

##lambda
f1 = lambda do |args|
  puts args
end
f1.call 'this is aha'

##Proc
f1 = Proc.new do |args|
  puts args
end
f1.call 'this is aha'

##Block, not sure
['this is aha'].each do |args|
  puts args
end

JavaScript

var f1 = function(args){
  console.log(args);
};

f1.call(this, 'this is aha');

They are very tricky when used as callbacks

Case 1:

def f1
  yield('passing args')
end

f1 do |args|
  puts args
end

Case 2:

def f1
  yield('passing args')
end

callback = lambda do |args|
  puts args
end

f1(&callback)

Case 3:

def f1(&cb)
  cb.call('passing args')
end

f1 do |args|
  puts args
end

Case 4:

def f1(&cb)
  cb.call('passing args')
end
callback = lambda do |args|
 puts args
end

f1(&callback)

The master says: yo, time to go to bed.

Advertisements
Standard