Josh Thompson     about     golden     archive     tags

Blocks and Closures in Ruby

Continuing on from yesterday’s post about method_missing, I’m moving on to a part of Ruby’s language that has been a bit of a mystery for me for quite some time. I’m still working through Metaprogramming in Ruby.

It’s the concept of lambdas, procs, blocks, and more. I also hope to better understand closures, and perhaps even some aspects of functional programming, as I’m used to (so far) thinking in OOP approaches.

I expect my first pass to be rocky, but to also lay a foundation for better understanding later. I’m writing this out because this is how I learn.

yield

First new term: yield. (docs)

It is always related to passing blocks around (to methods, or other blocks, procs, etc). I think lots of what we do with blocks interacts with the yield function, but it gets called behind the scenes. (Sorta how initialize calls, among other things, new)

you can call yield from inside a method to see any block passed to it. You can also call block_given? to see if there’s a block floating around anywhere.

def a_method
  return yield if block_given?
  'no block'
end

a_method
=> "no block"

a_method {"look at me, I'm a block"}
=> "look at me, I'm a block"

a_method do
  "i'm inside a do..end block"
end
=> "i'm inside a do..end block"
>> Read more

Metaprogramming in Ruby: method_missing

I’m working through Metaprogramming in Ruby

It’s a great read. There are examples in the books, but I wanted to take them out and apply them to some easy Exercisms.

I feel some disclosure may be useful. In no way, at all, should you ever implement any of the “solutions” I’m exploring here. I’m intentionally breaking things and doing them wrong to grow my understanding of how Ruby works.

If you’re following along, I’m doing the hamming_test.rb Exercism. It’s got a few tests. Here’s the first one:

# hamming_test.rb
  # ...
  def test_identical_strands
    assert_equal 0, Hamming.compute('A', 'A')
  end
  # ...

my working solution (in regular, non-metaprogramming ruby), is:

# hamming.rb
# ...
  def self.compute(string1, string2)
    raise ArgumentError if (string1.length != string2.length)
    counter = 0
    string1.split('').each_with_index do |char, index|
      if char != string2[index]
        counter += 1
      end
    end
    counter
  end
# ...

It’s a simple class method, takes two arguments, and even has an enumerable anti-pattern of using a counter outside the loop. This solution of mine is many months old, I’m not going to refactor that #each_with_index method. I know it’s itching for a #reduce, but let’s metaprogram it instead.

>> Read more

The How and Why of BlockValue

I’ve been sketching out an app that “crowdsources” GIS data.

According to Wikipedia,

A geographic information system (GIS) is a system designed to capture, store, manipulate, analyze, manage, and present spatial or geographic data.

My inspiration is Hoodmaps, which @levelsio kindly wrote an in-depth post about.

Armed with a solid example for something that I thought I could work with, I got to work.

The problem

In general, property delivers value to people by doing something. It can do all sorts of things, but my first pass was to try to figure out the relationship between buildings and parking lots. Eventually I realized I needed to be able to “tag”:

  • parking lots
  • buildings
  • green space (useful)
  • legally required but otherwise useless green space (building setbacks)
  • streets

and maybe more some day.

I’ve got a working hypothesis that we’ve built our cities based off of some insane rules, one of which being “lets put surface parking lots everywhere.”

I contend that a surface parking lot close to an economically productive stretch of property (like a walkable downtown) is a very poor use of resources.

Lets look at a quick example from my home town, Golden, CO:

Downtown golden

Hm. Doesn’t reveal too much, does it?

Maybe it would be nice if we could color-code the map by land use?

The solution

Lets take another look, through the lens of crowd-sourced GIS data:

Red is parking, blue is building

>> Read more

A Retrospective on Seven Months at Turing

Collection of thoughts on Turing

It’s the last week of Turing. I went through the backend software engineering program, and it’s been a journey.

In no particular order, I’m throwing down thoughts in three general categories:

  • What went well
  • What didn’t go well
  • What I might have done differently, were I to do it again

I believe there is a narrow window for me to capture this information well. As soon as I get a job, I’ll probably forget everything that didn’t go well, and if I were to never get a job, I’d forget anything that went well.

Went well

Learning

I’ve learned a ton, and my pace of learning has steadily climbed through Turing, as I build a more and more robust mental for existing information. It’s easier to add new information when the framework for the old stuff is strong.

I use Anki (a flashcard app) every day, so even on days when I don’t write any code, I’m still getting reps in of practice, and strengthening mental pathways. I’ve used Anki every day since Turing started, and have almost 1000 cards in my programming deck.

review stats

>> Read more