Rewrite JavaScript’s New – for learning purposes

I don’t suggest doing this at all. This is purely for the purpose of figuring out what the “new” keyword actually does:

not sure what “apply” and “call” do?

basically the same thing with different arguments =)

hash > if/else

If you have a HUGE if/else statement block. You should probably use a hash instead. Let’s draw up some context. Let’s say we were programing a library class. Depending on the status of a book you would want the library to do something.

That looks like a potential for a hot mess. Especially if we did not break out the code into smaller methods. BTW I’m a huge fan of small methods.

Let’s refactor that huge if block to:

Okay so what are we doing here? Welp we’re creating a hash that stores a bunch of procs. We’re relying on the state of the book to be the key of the hash and the proc to be the action we want to preform when the book is in that state. We’re passing ourselves in, ourselves being the library instance, and the book object ( some methods require it as an argument ). This reads a lot nicer.

The more OO (object oriented) approach would be to replace the procs with classes that do specific things according to the book’s state. But I didn’t want to show that here because the abstraction would get out of hand.

What are you thoughts? Got a better idea?

Actually extend really is include

Last post I explained the difference between extend and include. But more digging around and low and behold, extend and include are pretty much the same thing, in different scopes. Okay so bare with me here.

Every object in ruby has two sets of methods, instance and class methods. Instance methods are passed down to any new instance. What ruby really does in the background every time you do it creates a 2 objects. One being your instance and another being a metaclass or a singleton_class. The reason behind this is because you can define methods on instances on the fly. I.e.

Without a metaclass in between the instance and the class we would have to open up Foo again and define the instance method. But this will affect all instances of Food not just foo. This is why the metaclass exists between the instance and the class. When we define a method on an instance we’re actually opening the instance’s metaclass and defining a instance method in it.

I lost you there. #=> instance of Foo + metaclass (hidden)
Right now inheritance is now Foo => instance_metaclass => foo (instance)

When we do def foo.hi we’re actually placing an instance method on instance_metaclass and not Foo.

Not to get weirder but a class also has a metaclass. So inheritance again:

otherstuff => otherstuff_metaclass => Object => Object_metaclass => Class => Class_metaclass => Foo

Because we do class Foo end, what we’re really doing is Foo = and as we have already discussed. Whenever ruby makes a new object, it actually makes 2 objects, the obj + a metaclass.

Okay let’s go over from the end. Say we have:

And we extend it in class A. A.hi will work. But notice if we did:

A.hi will still return ‘hi’. super implies that it is calling the method on an inherited class. But if we do A.ancestors, B does not appear on the list. Weird? nope because if we look at it’s meta class, it’s there.

Now to prove back to the point that extend really does include but in a different scope we’re going to extend A with module C and you’ll see C is in the list of A’s ancestors.

So really extend is include, just in a different scope.

Ruby > extend vs include

In attempt to dry up some code I was building a tree of modules without much success. I’m going to write this down because I KNOW I will do the same mistake tomorrow.

Cool. So ExampleIncluded.greeting won’t work and will work. And vice versa for ExampleExtended. Okay here’s why.

When you extend a module you’re basically saying. Hey module all of your instance methods ( methods defined like so: def foo; end ) add those as class methods. When you include something you say hey module add your instance methods to my instance methods list. Cool so far so good. What I was trying to do without much success:

Welp I wanted to call ExampleIncluded.share_also. But nope that gave me a no method error. Instead I had to call ExampleIncluded::Common.share_also. I get a nice fancy warning along with it as well: “Warning top level Constant referenced by ExampleIncluded::Common” but at least it runs share_also

Basically you have to include/exclude modules individually. To rewrite this:

Not entirely sure why it’s implemented this way, but I suppose it makes you more aware of what is going where. Seems a bit odd to me though why you would be able to call ExampleIncluded::Common.share_also. I’m still pretty new to this, so if I’m wrong by all means please let me know =)


Okay so I did some reading and meta_programming FTW

So there is a call back that exists in modules that runs whenever you extend or include a module.

The parameter that included receives is the module/class that includes module A. Well what can I do with that? You can create class or instance methods!
So when we’re inside of the block (where I commented #Do crazy stuff):

This will actually create a class method. But what if I want to create an instance method? Welp:

BOOM we just made a instance method on a class. The reason we have to use the send method instead of just doing mod.define_method is because define_method is a private method. Yeah there’s a way around that too. Wrong way? Please correct me!

Five months in and still learning

Yup fell through with this blog thing haven’t I?

What have I learned from the last time I posted something?

– Twitter is awesome, I just witnessed an interesting conversation about reactor pattern and threading in ruby. It all started by asking a simple question.

– Reactor pattern.

– Node is built on V8

– Rails Engines

– Volunteering at devOpsDays

– exercise is a must!

Have not learned workman/Dvorak. Some may argue that it may be a disability to type faster. Even if I did know how to type faster would I be able to think code fast enough to keep up? My mind can barely keep up on what I am typing!

Have not been able to read enough books =\ no excuses.

I want to rebuild this blog in rails. That way I can learn the devOps side of things.

Ruby – Metaprogramming

I started working at Mass Relevance last week four weeks ago ( I have been slacking on this blog post ) and I have learned a ton! Specifically how awesomely flexible Ruby is.

1) Private methods can be access via send method.

2) Define/remove methods on the fly

3) Including modules on the fly

4) Adding a variable module on the fly
I didn’t actually read about this, kind of just put the pieces together.

5) defining instance methods on specific instances
I made a mistake which then led me to this discovery

Mind BlownEmma Stone - Mind Blown
Mind blown yet? Yeah mine too…oh and I’m a cat owner if you haven’t noticed by now…

Lazy Evaluation

I’ve heard about AMD ( asynchronous module definition ) before but until recently haven’t really looked into it. One of the cool things I read about was “lazy evaluation”. Say you have a ton of javascript, but the user never uses it because he doesn’t perform xyz task. Well you just wasted a whole bunch of time loading the script that never got used. So let’s do something hacky with it.

JS can evaluate strings. What that means is…

This will run an alert with the words, this will run. How cool is that? Go ahead and try it! Copy paste it into your console. Please don’t make a liar out of me script!

So let’s say you have some cool animation script and it’s huge! Like 1mb! If the user never clicks on the play button, they don’t need the script, well TOO BAD!! The browser loads it anyway! Well if we wrap that animation script into a string the browser reads it but doesn’t process it, much like my days in English class. This saves a ton of time! So what happens when the user clicks play? We evaluate the string, duh! We just saved her a bunch of time on the front end. Even more so if she didn’t want to see our sad animation in first place!

The article I’m reading

Just on a side note, Ruby has a similar method. eval(string)…shocker…

While applying for jobs… I found a bug in a form.

So while out applying for a web-developer job I came a across a form which would not let me submit. It keep scrolling me back to a field which I had properly filled in. It didn’t have any error messages or anything. So I tried refreshing the browser which promptly killed all of my entries. So I refilled everything in again. Nothing. So I got smarter this time, I opened up a new window and did the old command-c command-v, perhaps I missed something! Nope. I got frustrated and some-what embarrassed. So I decided to see exactly what was stopping the form from being submitted. Into the javascript I dove and well… I found it….

The form wasn’t submitting because the URL I was giving it started with https// instead of http//. This is a small and dangerous oversight. Especially because the field was asking for a website that generally uses SSL. Imagine if I were a client or a potential client. I probably would have given up after the 2nd try.

Lessons learned here: 1) MAKERSQUARE works! 2) Try to think of all possibilities and anticipate edge cases especially with forms. Have someone try using it. I typically ask my non-technical fiance. She tends to break anything electronic. Which gives me the perfect opportunity to be the hero of the day =) otherwise I’m useless around the house…sort of like a cat. 3) Looking at how other people set up their code is really really interesting. After seeing the script of this form I have a ton of questions for my instructors at MakerSquare =)

Keep learning, keep growing =)

Dear Cohort > 2, Take it from me!

I’m writing this post MakerSquare at 7:39AM the day after Career Day. Yes I know we don’t have class until 1pm. Yes I know I need to rest. And yes it has been an exhausting past few months. Little workers in my brain are running around figuring out where to store all the code mana`o I have gain the past few months. My brain is swollen. But this isn’t the end of the journey. This is the end of a small part of the beginning. I think now is a good time to share with the next cohort what worked best for me.

1: Sleep
It’s hard to believe I get any sleep. My round trip commute from Temple is about 3 hours a day. I leave home around 5:30AM and don’t get back until 8:00PM if I’m lucky. But no matter what, I try to get at least 7 hours of sleep. Your brain needs a timeout to grow and store all that brand new information you’ve just learned. You’re right, you could stay up to study, but are you really studying at your optimum level had you had 7-8 hours of sleep? I doubt it and so do experts around the country. You’re tired. You can’t focus and you quickly forget things. Remember where your keys are? Go to sleep! You’ll feel better and be more productive. Don’t believe me? Check out CDC’s website.

2: Exercise
This one was particularly difficult for myself. Because of my long commute I don’t have all the time in the world to exercise. So instead I bought a yoga DVD, AM Yoga. It’s 30 minutes and enough to get the blood flowing so my body doesn’t cramp up. Towards the end of the course I got sloppy and didn’t keep up with the yoga. My body complains more than I want to hear. My posture resembles a C more than an S. Needless to say I’m looking forward to going back to yoga, weight lifting and running on a regular basis. But why is exercise essential to learning? From what I remember in Brain Rules, exercise increases the amount of oxygen you intake. Cardio of course is the best, but John Medina even suggests walking 15 minutes a day will help the increase the flow of oxygen. Apparently our brains need a lot of that…

3: Life outside of code
Do something fun, do something exciting. Code will be a part of your life for the rest of your life. Don’t worry about it, it’ll still be here when you come back! For me it was spending time with the fiance and I couldn’t ask for anything more, even if she made me watch Mama Mia with her.

4: Code without an excuse!
The great thing about code is that the only thing you really need is your computer + google. When we first started to learn rails I needed setup a rails app without taking the shortcuts (scaffold, resources routes etc). It forced to me to really think about how the MVC model really worked. Okay I lost you. Remember this, learn code the hard way and you’ll learn faster and more efficient.

5: Learn how to use iTerm/Sublime hotkeys
Hotkeys are essential. The less time you spend gliding the mouse all over the place and the more time you keep your fingers on your keyboard the more efficient you will become. If you put two devs side by side talent being equal but one uses her mouse to navigate while the other strictly uses her hotkeys, the latter will be more efficient.

I did a screencast to prove my point. It’s my very first screencast. To be honest it’s really nerve wrecking, even if I’m not doing it live.

Here are my favorite hotkeys:


Search for files in your project directory: Command + t
This is vital when you begin to have a full grown web application. Thanks to the nature of inheritance of OOP, you’ll have a ton of different files with a ton of different code, navigating through that mess will save you tons of time!

Search for text in all files: Command + Shift + f
Say you’re looking for that one typo that is breaking your project. Or let’s say you forgot what this one particular message does. You can search for a specific text using Command + Shift + T and sublime will search all of your project files for whatever you give it. It can even take regex, sorry didn’t know you’re a BOSS!

Auto complete tags in HTML/CSS: tag name + tab
Let’s face it, html tags = pain. So type html + tab and you’ll have open and end html tags, LIKE MAGIC! Lorem ipsum works the same way, lorem + tab


Auto-complete – tab
Say you’re trying to get into a directory called supercalifragilisticexpialidocious, I had to Google it to make sure I spelled it right. No thanks, let me just type super hit tab and iterm will fill in the rest. Super! Let’s say we have another directory called super, no problem! iTerm will show you that there are two directories with similar names, hit tab again and you can choose which one with your arrow key or you could continue to type until the path name is unique.

Open directory or file in finder – open (filename or . for current directory)
Instead of clicking and dragging your file into chrome, instead type open index.html in iterm!

SizeUp (Hot keys are listed when you click on the icon on the top part of the menu.)
Manage your windows properly so you can have two things open side by side. I.e. Sublime + Chrome. Don’t know what I’m talking about? Can’t find it? Ask any instructor at MS and demand they tell you this 10,000 year old secret!

There are wayyy too many hotkeys for me to list, some I bet I don’t even know! But these I found to use these the most often.

I showed you mine, now show me yours! What are you favorite studying techniques or hotkeys?