Ruby: or, how Wayne saved me from PHP

So after reading my previous post about Learning To Be A Web Programmer, Wayne called me up on the phone to talk about where I might start. As you all know, he’s sort of a big deal and a super smart technical ninja, so I was happy to work out some ideas with him. The conversation went something like this:

  • Wayne: Where were you thinking of starting?
  • Me: Well, I was thinking maybe PHP and mySQL.
  • Wayne: eww. Don’t do that.
  • Uncomfortable pause
  • Me: Is there a better way?
  • Wayne: Yeah man, haven’t you heard of Ruby on Rails?
  • Me: That’s like PHP, only Ruby right?
  • Wayne: Dude, it is so much more…

He kind of left it mysterious like that, but with my interest sufficiently piqued I went out and did a little research today.

I was familiar with Ruby as a scripting language from many years ago, back when the big thing was that Ruby leaks memory like a boat made out of swiss cheese. I mean, not from personal experience or anything, it’s just the kind of thing you hear when you work with a bunch of geeky programmers all day. Word on the grapevine was this Ruby thing was a neat new scripting language with nice syntax, had some problems, but also had a lot of potential.

Well, if that wasn’t an understatement.

Coming back to it now apparently it’s a completely different ball game. Ruby is all grown up and powering a bunch of my favorite websites. Well that’s great, but what the hell is it, really? Is it as I thought, just ruby running on a server, interpreting http requests, generating HTML, and serving it up?

Whenever I try and answer that question, I get a lot of people copy pasting the paragraph out of ruby on rail’s website stating that it’s a “a web application development framework” which means all of nothing to me. So I guess I need to start actually using the damn thing.

In my next installment, I’ll begin digging a little deeper with Ruby on Rails, starting with installing and configuring a local copy of Ruby and Rails on my machine, and creating my first Rails application (maybe?). Oh and bonus points if I can actually figure out what the hell it is.

See you next time,
-Howard

Learning the Web

Today I went to Barnes and Noble for the first time in years. I really didn’t need anything because I’ve been getting all of my books online, via Amazon mostly, but I was in the area and decided to drop in and check out the programming section for nostalgia’s sake. You see, back when I was first learning how to be a programmer my options for literature were a little limited. I didn’t have internet access at home, and the local library didn’t have any programming books, and even on the public computers with internet access the articles on programming were few and far between, so I would sit for hours in the local Barnes and Noble and read a chapter at a time, sometimes writing code on napkins or in a notebook to take home. (And I don’t mean netbooks either, I’m talking Mead Composition notebooks crammed in with my Algebra assignments).

It was here I read all those Andre LeMothe books, Sam’s Teach Yourself C++ In 21 Days, and the O’reilly books – curled up in the aisle, feeling slightly criminal, especially since it was years before I ever actually bought a book.

Anyway, when I went back today, you know what I saw? Only a single shelf for native C++ developers.

And you know what else? It’s not just bookstores. Look at LinkedIn job boards, or any major software development career page. Web development is the place to be and engineers who don’t know at least the basics of relational database theory or some PHP or Java or Flash / ActionScript are at a real disadvantage in the current job market. That’s not to say there aren’t any jobs for us native coders, but it really does close a lot to doors for you.

Now, I’ll be honest, there was certainly a phase in my life where I looked down on web developers with disdain. The 13 year old Howard would turn up his nose and laugh at those script kiddies and their PHPs and SQLs and whatnot. Real programmers wrote native code! But the truth is, an older, wiser, and less-of-an-asshole Howard realizes that the mistake has been his all along. Aside from being an elitist prick, I had inadvertently violated the single cardinal rule of programming:

Never stop learning.

It’s simple really, I’ve avoided learning any web technology for over a decade. Sure, I wrote PHP back when it was PHP3. I think I used it to use “includes” in my otherwise HTML webpage. I used java once to rotate a banner. I learned HTML before

tags were popular (actually, I’m still a little fuzzy on how that works exactly, I just used raw tables). For all intents and purposes, despite the many years I’ve considered myself a good programmer – I’m a miserable excuse for a web developer. I’ve stagnated. I found a place where I’m comfortable, and I’ve dug myself a rut.

Nothing gets you out of a rut quite like public embarrassment.

So I come before you today a noob web developer looking to cut his teeth and learn to write some web technology. It’s going to be shockingly revealing, a little bit shameful, but hopefully a little entertaining and educational along the way. It’s going to be part oped, part tutorial, and part diary – and with any luck at some point, I might actually be able to count myself among the web development community.

See you soon!
-Howard

Conversational Programming: How To Use The Percent Sign Operator in Python

The Percent Sign (%) is a interesting beast in the Python language.  It does something called string formatting and it’s a mathematic operator as well.  Whatever math is.  Let’s find out what the POWER of the % is… first, we’ll learn by finding out what NOT to do.

print %
^
SyntaxError: invalid syntax

Easy fail.  What’s next?

print "foo" % "bar"
TypeError: not all arguments converted during string formatting

Formatting?  I barely knew her!  Ok, how about using this thing for real.

If I wrap the following text in PRE tags, WordPress munges the entire post. Yum!
print “whiskey%” % “tango”
ValueError: incomplete format

I’ve got more ways the percent operator can fail.  Feel free to post others in the comments section.

TypeError: float argument required, not str
TypeError: %d format: a number is required, not str
ValueError: unsupported format character 'X' (0xXX) at index X

Why doesn’t this damn percent thing WORK?!!?!?  Truth is, there’s a million ways to use the % operator incorrectly.  To make it worse, exactly none of these errors were super helpful readable english, unless they got you here.

So here’s how you use it:
Modulus operation: Performs division like the typical forward slash (/) operator, but instead of returning the result, it returns the “remainder”:

16 % 4
0

or

16 % 5
1

Awesome!  Lesson learned?  Good, because you will never use modulus.  Feel free to forget everything I’ve said above.  Do not forget anything that follows.

String Formatting:

print "Holy Shit"
Holy Shit

Ok, that didn’t have a percent sign in it.  Just keeping you interested.

StringyStringerton = "Beets."
print "Fact: Bears. %s Battlestar Galactica" % StringyStringerton
Fact: Bears. Beets. Battlestar Galactica

The percent sign living inside the string says, “Wait up.  I’ve got something interesting to tell you about.”
The very first character after the percent sign says what type of thing you want to say.

Please read the last paragraph again until you truly understand it.
Moving on. d is a Digit. f is a Float. s is a String. There are others, but I’ll go over these first.

How about this:

Bitches = 99
print "I have %d problems." % Bitches
I have 99 problems.

Change it up.  you don’t need to set the variable first!

print "I like %f numbers" % 100
I like 100.000000 numbers

What if I want to put leading zeros or padding before or after my numbers?  Padding is easy.  Put a <SPACE> before the percent sign, and a number after it.  Like this:

print "I like %10s spacing" % "nice"
I like            nice spacing

Leading zeros is easy.  Put a zero after the percent sign, followed by a number.

print "I love %05d woman." % 1
I love 00001 woman.

I can’t keep your attention forever, so I’ll make this brief.   You can use unlimited string formatting operators (%), just separate them with a coma.

print "I love %s, but hate %s." % ("Honey Nut Cherios","milk")
I love Honey Nut Cherios, but hate milk.

Templates is a better way to customize HUGE swaths of text… like a mail merge, if you’ve ever used it.  I actually haven’t.  Pity me.

There is official documentation about the infamous string formatting operator over at Python Docs.  It’ll give you a good list of things to use after the percent sign to format things and wrap your head around a pole.  I’m pretty darn sure that I broke WordPress with this post.  So I’ll…

Yep!  I need something better for writing about code.

Hello World!

I’ve created #import flying to document my quest to find the answer to the following question:  “Can a technical person with neophyte programming knowledge (that’s me!) study long enough to become a recognized expert coder?”

The name itself is inspired from an XKCD comic, which got me interested enough to learn the basics of Python.  I have mastered the art of copypasta just as well as any other Python developer.

The goal of this site is to simply share knowledge as I learn new things about programming.  I’m interested in various areas, but my main goal is to make coding a full time job.  It will take perseverance to accomplish my goal.

Just to include some usable content in the first of many self-mastabatory posts, there’s a brilliant man named Richard Buckland who teaches higher computing at UNSW, and he has some amazing lectures up on YouTube which blew my mind and kept me entertained at the same time.

Note to future-self:  This is where it began.  Stay away from Reddit, god dammit.