Day 21: Hiked in NZ while in Singapore!

Nick Ang
getting technical
Published in
4 min readJul 21, 2016

I’ll get to that in a bit. First, what I learned today.

Ruby lessons to internalise

  • Ruby proc
  • Ruby yield

Ruby ‘proc’

This is embarrassing. I googled a bit and couldn’t find out what ‘proc’ stands for. Usually these operator/method shorthands mean something but I can’t think of anything besides ‘process’, which doesn’t seem right at all in this case.

Proc is new to me. JavaScript doesn’t have anything named procs. But I think I understand the concept behind it.

cube = Proc.new { |x| x ** 3 }

This creates a proc object that holds the code ( |x| x ** 3 ), and this proc object is stored in the variable ‘cube’. A proc is basically a reusable chunk of code (also known in Ruby as a ‘block’) that can be passed as an argument to a method. (Method in Ruby is like function in JS.)

If instead of passing a proc we pass a second method as an argument to the main method, a scope problem will arise. As user102008 explains, “defining a method inside another method definition will not give access to the local variables in the outer method body… [while a proc] captures local variables from the enclosing scope where it was created, and you can use those variables inside the closure.”

An example of a proc in use:

floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911]

round_down = Proc.new { |x| x.floor }

ints = floats.collect(&round_down)

Here, the .collect method is passed the pseudo-argument ‘round_down’, which is really a proc. To let a method receive a proc as an argument, we put a ‘&’ operand in front to signal to Ruby that this proc can be and shall be accepted as an argument here.

I like the idea of a proc. It keeps the code more modular and easier to access, and it makes writing code that use repeated ‘blocks’ a lot easier. All this keeps in line with the DRY philosophy: Don’t Repeat Yourself.

Ruby ‘yield’

Yield is also new to me and I have to admit it is not intuitive to me at all. This RubyMonk article helped a lot.

Yield is a keyword used inside a method definition to temporarily exit the method with its parameters in tow, hand them to a ‘block’* for it to do stuff with, and re-enter the originally called method and pick up where it left off with the processed parameter values.

Why not just write the code inside the method itself rather than having to transition out, run some code, then return the values into the method? Because the method definition is a holy place, and code inscribed within it is not malleable. In contrast, by sending the parameters received by the method (through a method call) to the block to be processed, the programmer has the flexibility to write different blocks to work in conjunction with methods. Less absolute code means greater adaptability.

*(Again, a ‘block’ is a chunk of code like an anonymous function in JavaScript that, crucially, cannot be invoked again without rewriting the whole block elsewhere.)

Hiking through NZ in a Singapore gym

Went to the gym today to do some high intensity training. I mistook the class start time so I climbed on a Stair Master thingy and just started climbing, or pedalling if you’re a strict gym rat. What a pleasant surprise when I found out about the courses that the machine offered!

This is 2016, and all the equipment that could possibly accommodate screens have screens on them. Without thinking much, I tapped on the ‘Course’ icon on my screen and completely unsuspectingly launched myself onto hiking trails in New Zealand! The footage looks like it was recorded with a low-flying drone. That was a great experience. The whole thing felt 80% real, 20% simulated (because it was after all on a flat screen).

Hmm…

Potential for VR

It would be amazing to experience this on the next level with virtual reality headsets. Enough said!

10 days left

July has only 10 days left. Ten days is enough time to do a lot, but not that much either. A few days earlier I noticed the fruitfulness of having a problem to solve in accelerating my learning when I tried to resuscitate a small DC motor salvaged from a Sharp vacuum robot I tore apart. Now I’m thinking of a problem worth solving to ramp up my learning pace again.

Tomorrow I’ll decide on one to work on for the remainder of July.

Oh Google…

Umm, not quite google, not quite.

Not sure what the thing is (that’s why I used google image search in the first place), but I’m pretty sure that is not a piece of jewellery. It’s probably my feminine fingers.

This post is part of my 30-day commitment to write about my journey learning technical stuff. If you learned something, please recommend it so that more of us can share our learning.

Posts are published to Getting Technical.

--

--

Nick Ang
getting technical

Software Engineer. Dad, rock climber, writer, something something. Big on learning everyday.