DevHeads.net

Postings by John Carter

Thinking Aloud: Refactoring from nested loops to enumerators and lambda's

So I'm just thinking out aloud here.

Being an Old School Programmer I tend to naturally to write ever more
deeply nested loops.

I hate myself when I do this because it’s hard to test especially if
some of the loops have nasty external side effects, it’s hard to
reuse, it’s hard to refactor.

So I’m trying two new patterns....

* Passing enumerators as parameters so I can pull out inner loops as
standalone functions without calling the entire function every time..

* Creating functions that return lambdas, so I can pass an inner loop
in as a parameter.

Walk with me this is going to be long….

Metaprogrammatically defining methods with required, optional, required keyword and optional keyword parameters?

I'm want to do meta programming in the sense of writing ruby to create
a new ruby method.

Module#define_method exists....

<a href="https://ruby-doc.com/core/Module.html#method-i-define_method" title="https://ruby-doc.com/core/Module.html#method-i-define_method">https://ruby-doc.com/core/Module.html#method-i-define_method</a>

However modern ruby now has required parameters, default parameters,
required keyword parameters and optional keyword parameters and an
optional a block parameter.

So Method.parameters seems to understand all these things....

How does the class name magic with Class.new?

This seems like deep magic... Any idea how it works?

a = Class.new
=> #<Class:0x0000558d34f68b48>
It seems as if whenever you create a Constant it checks to see to see
if it is an unnamed class and then names it....

That seems..... too magic.