Bitcoin maximalist.

Thoughts & Technical Writings.

Scope in Ruby Is Like a Cell Membrane

| Comments

How is program scope like a cell membrane?

Scope is the context within which something is defined. Let’s focus on methods, for starters. If you write a page of code with a method in it, alongside a bunch of other stuff (variable declarations, enumerators/iterators not contained in that method, etc.), don’t expect your method to have any clue what’s going on above or below it in that page. Sorry, that explanation sucked - let me show an example:

some_numbers = [1,2,3,4,5]
another_variable = {:cell_membrane => "cytosol", :receptor => "ligand"}
more_variables = some_numbers.collect do |num|
    num * 2

def method_in_same_code_file(args)
    more_variables + some_numbers ##=> This method doesn't know what these are!! 

The way that we get around this issue of restrictive method scope in Ruby’s design is actually something we all are quite familiar with: passing arguments to our methods. Perhaps you can imagine now the similarity to a cell membrane - take a look at the image below. I would sa the receptor-ligand binding is a lot like doing something like this:

<page of code omitted… let's just call the method>
method_in_same_code_file(some_numbers, more_variables)

In this method call, our arguments works just like our receptor-ligand binding! Put differently: the arguments function as the cell membrane’s receptors, in that only by going through them can stuff on the outside be itelligible/readable to the method’s insides.

Method scope visualization!

cool things Kent Beck has said (about programming):

One line methods are there to communicate.

You don’t spend three or four lines expressing iteration, you spend one word.

YAGNI: You aren’t going to need it.

Optimism is an occupational hazard of programming; feedback is the treatment.

(Speaking about his Chrysler experience…) By far the dominant reason for not releasing sooner was a reluctance to trade the dream of success for the reality of feedback.

Make it work, make it right, make it fast.

how to write a basic test/spec:

def assert_equal(actual, expected)
    unless actual == expected
        puts "fail: expected #{expected} but got #{actual}"

Not so bad, right?