Learn With Jeff

I learn by teaching

Goals for the Month of June. - Learn With Jeff

In my learning, I find it immensely helpful to set lofty (but possible and measurable) goals. I didn’t my goal for the month of May but it was pretty straight-forward, publish a gem. I accomplished that goal, twice.

I’ve decided to talk about my goals out for the month of June since they are much more challenging than my goals for May. First, I want to publish a “useful” gem/plugin/program/app. Second, I want to make some contribution to an open source project.

Both of these are a little subjective, so let me define further. I define “useful” as someone other than myself uses it. Not just downloads it, actually uses my creation. Now, since I can’t control whether or not my contribution to an open source project is accepted, I will be a little more loose in that definition. I want to issue a meaningful pull request on a public repo that has multiple contributors.

I realize that the above are huge challenges for a beginning programmer, but I wouldn’t get anywhere if I only tried to do comfortable things :)

Testing - a N00b’s Reflection - Learn With Jeff

I have officially spent more time testing and refactoring my code for my black jack game, RubyJack, than I have on the original code base and it has taught me a big lesson: TEST FIRST.

This is a topic that I don’t have the breadth of knowledge to really attack. Jeff Atwood does a much better job than I, here. I do, however, feel that I can add the perspective of a new programmer.

The challenge of testing first as a n00b, is that when you are learning to program, you generally learn

    def print_hello_world


      puts "hello world"

    end

    print_hello_world

before you learn

    it  "prints hello word" do

      print_hello_world.should == "hello world"

    end

I wouldn’t say this is a mistake since testing is inherently more complicated, but it does create a significant barrier to entry to the new programmer. This barrier is mainly a comfort level thing.

When I was first learning about testing, I thought “I can really see the theoretical application of this, but my programs are so simple now that I don’t need tests.”

BIG F&%$ING MISTAKE.

The big example, in RubyJack, of where writing my tests first would have helped me, was during the refactoring process. The first draft of my code may not have been bad for a new programmer, but was(is) poorly written software on a whole. There were methods in classes where they didn’t make sense, arrays of strings where they should have been arrays of objects, 25 line methods that did one simple task and many, many other problems.

Up to this point my tests consisted on running the program through my command line, playing the game till it broke and reading the error message. Not super efficient.

So, after I published the first version of the gem (gem install RubyJack) I started to move stuff around. First, I did some simple refactoring of individual methods, no problems. Then I thought “oh, well this method that starts the game probably should be in the ‘Game class’ and not the ‘Player class.’”

All Hell Broke Loose.

Nothing worked anymore, I couldn’t even start the game let alone deal a card or initialize a deck.

Thank god for “git reset –hard”.

I was able to hop back to my latest commit and start again (more about using commits to your advantage later). Then, I wrote some tests. It was hard;  it hurt my head; I didn’t like it.

I struggled through them and got (most of) them to pass. Around this time I brought in some professional help, Gavin. Gavin was nice enough to sit with me and help me do a little (a lot) of refactoring of my code. We moved some methods into classes where they made more sense, and created classes that should have been there from the beginning.

Then I tried to do some more refactoring on my own, which led to even more problems. I hadn’t learned my lesson.

Once again, I found myself reaching for “git reset”

It was then that I resolved to write better tests. I re-watched the pragmatic studios video on unit testing and peppered Gavin with some more questions. Then, I wrote a bunch of tests and MADE THEM ALL PASS.

When I originally wrote my tests, if I couldn’t get a test of a certain functionality to pass, I marked it as “pending” instead of figuring out the problem. Not this time. It took me hours, and until 2am, but I made them all pass.

I then entered the process to which Gavin introduced me to:

Red

Green

Refactor

What that translates to is this:

Check my tests, if they pass I make one small change. I then check my tests again, some of them may be failing, I want to make sure they are the ones I expect. Then, I continue tweaking the code till I get it to pass all the tests again (sometimes tweaking the tests too).

Then I start the process all over again with my next change.

The benefit to testing first, as I see it, is as it concerns the thought process towards your code. As J. Timothy King States:

You ask, “What code will I write to generate a solution?” But that’s backward. The first thing you should be doing… The first thing you ask is not“What code will I write?” The first thing you ask is “How will I know that I’ve solved the problem?”

So, my tip for other n00bs out there is to learn to test sooner rather than later. You won’t regret it.

Thank You!

I want to give a huge public thank you to Gavin Stark for helping me with the refactoring process and answering all my crazy n00b questions. Thanks Gavin! Also, thanks to Aubrey Goodman who graciously takes my questions at all hours. Finally, I’d like to say thanks to Andy Lindeman who went through and commented on my code on github! It is people like you guys that make the Ruby community awesome and make me glad to be a part of it.

Disclaimer, links, and tweets

I love to learn and I do not pretend to be an expert on the topics I post about. If I’m wrong please, let me know in the comments, I love feedback and you won’t offend me. Follow me on Twitter; I love to discuss Ruby, books and anything interesting.

If you want to help an aspiring Rubyist, I’m on GitHub and would love any feedback you have to offer.

Many people have helped me learn, I would love to return the favor! Get in touch and I’ll see if I can help.

Thanks for reading!

RubyJack My Second Ruby Gem! - Learn With Jeff

For the last two days I have been slaving over my second attempt at a real ruby program, this one with no help from my friends over at pragmatic programming. I am going to briefly go over something I learned doing this, some of the problems I faced and some of the features I am still working on.

If you want to find the project I am working on, the source code can be found here on my GitHub profile.

You can install the gem with $gem install RubyJack

TIL:

Today I learned a little bit about ruby’s regular expressions. I  know there is a lot more that I don’t know about them, but from what I can tell, they are powerful. The most basic definition I could find of a regular expression is from the Programming Ruby 1.9 book (the latest PickAxe addition):

A regular expression is a pattern that can be matched against a string. It can be a simple pattern, such as the string must contain the sequence of letters “cat”, or the pattern can be complex, such as the string must start with a protocol identifier, followed by two literal forward slashes, followed by…, and so on. This is cool in theory. But what makes reqular expressions so powerful is what you can do with them in practice:

-You can test a string to see whether it matches a pattern -You can extract from a string the sections that match all or part of a pattern. -You can change the string, replacing parts that match a pattern

The job I needed regular expressions for in my code is most likely based in the poor design of my deck of cards. The following is the code that initialized a new deck:

def create_a_deck
  new_suits = %w(hearts spades clubs diamonds )
  new_value = %w(Ace 2 3 4 5 6 7 8 9 10 Jack Queen King)
  @new_deck = Array.new
  #creates a new array by combining the information in the
  #above two
  new_value.each do |card|
    new_suits.each do |suited|
      @new_deck << "#{card} of #{suited}"
    end
  end
end

What I am doing above is creating an array of strings that have combined the strings found in the “new_suits” array and the “new_value” array above to make a new array that includes an entire deck. The iterator starts with “Ace” and combines it with “hearts” to make “Ace of hearts” which is then added on to the end of the @new_deck array. The iterator goes through and makes 52 cards in this fashion.

The problem with this method of making a deck is that all of my cards are now Strings and I need a way to covert them into point values for the Black Jack game. I did some research and came upon regular expressions.

Here is the method I used to parse the deck and assign values:

def card_value card
  if card =~ /Queen/
    10
  elsif card =~ /King/
    10
  elsif card =~ /Jack/
    10
  elsif card =~ /Ace/
    11
  elsif card =~ /2/
    2
  elsif card =~ /3/
    3
  elsif card =~ /4/
    4
  elsif card =~ /5/
    5
  elsif card =~ /6/
    6
  elsif card =~ /7/
    7
  elsif card =~ /8/
    8
  elsif card =~ /9/
    9
  elsif card =~ /10/
    10
  else
    0
  end
end

I have a gut feeling that any rubyists that are reading this, died a little bit inside with that method. However, it got the job done and it taught me a little about regular expressions. Essentially what this method does is search the string I give it (I’m using this with an iterator and passing it each card in the player’s hand) and assigning a value based on what it finds. I will probably add another method to this in order to expand upon the Ace, but more on that later.

Problems I faced:

So there are a couple parts to blackjack that were a little tough for me to get in the program. They are, dealer play, the option of playing an Ace as a 1 or 11, and the ability to bet.  I am not going to attack the betting capability just yet but I do think I will discuss dealer play and  Ace action.

Dealer Play:

On the surface, this problem seemed pretty simple. The dealer is just a special player. So, create a subclass of player and write some methods that automate the play of the dealer (I won’t paste all that code here, go look at the source!). However, every time I called a method from the dealer class my program crashed. I have to keep fiddling with that to see if I can get it to work.

Ace Action:

I actually think I solved this problem while I was writing this blog post (which is why I write this blog). I think if I add call a method in when the card_value method finds “Ace” that prompts the user to make a choice, it will solve the problem. The tricky part will be offering that choice repeatedly (so if the user changes his mind later in the hand) and automating it for the dealer.

Features to come:

So you can probably gather that I plan on adding the above two features today. Another feature that I want to add is the ability of the user to place bets, have holdings and play multiple games.

Please go play the game ($gem install RubyJack) and give me some feedback! Thanks!

I/O…FINALLY! - Learn With Jeff

When I was learning to play the piano, I remember wanting to jump right into Billy Joel. “Hot Cross Buns” and “Mary Had a Little Lamb” were boring and very uncool for an eight year old boy to be learning.

I have had the same feeling while learning to code. I spent a lot of time doing methods, objects and classes never really getting to use them as I would in the real world.

There is one question I wanted to be able answer since the day I started programming: “How the hell do I interact with a program?”

Today, I figured that out. The code below is from the main program file for the game I created in the Pragmatic Studios course. In it, there are two examples of some of ruby’s basic input functions.

knuckleheads = StudioGame::Game.new("Knuckleheads")

default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
knuckleheads.load_players(ARGV.shift || default_player_file)

loop do
  puts "\nHow many game rounds do you want to play?"
  answer = gets.downcase.chomp
  case answer
  when /^\d+$/
    knuckleheads.play(answer.to_i)
  when 'quit','exit'
    knuckleheads.print_stats
    break
  else
    puts "Please enter a number or 'quit'"
  end
end

knuckleheads.save_high_scores

The first input I learned was the “gets” method. Here, the program prompts the user to provide a number, the code checks that it is in fact a number (or a quit command) and then uses the number as an argument for the method.

loop do
  puts "\nHow many game rounds do you want to play?"
  answer = gets.downcase.chomp
  case answer
  when /^\d+$/
    knuckleheads.play(answer.to_i)
  when 'quit','exit'
    knuckleheads.print_stats
    break
  else
    puts "Please enter a number or 'quit'"
  end

The next one is a little more complicated so I will try to break it up into pieces.

What I am trying to accomplish is to allow non-programmers to add players to my game using a csv file and no code. They should be able to simply input the name of a player and the program automatically adds a Player object to the game.

To do this I created a player.csv file (in the above code a person could theoretically create their own file and load it in) with a series of players each on their own line. Then I included the following code in my Game class:

  def load_players (filename="players.csv")
    CSV.foreach(filename) do |line|
      player = Player.new(line[0], line[1].to_i)
      add_player(player)
    end
  end

This takes a file (defaulted with my players.csv) and goes through each line of the file, creating a Player object for each one.

Then, I included this in the main program file:

default_player_file = File.join(File.dirname(__FILE__), 'players.csv')
knuckleheads.load_players(ARGV.shift || default_player_file)

This loaded the players from the file given by the user or my default file if none is give and then runs the program as normal.

So, after two months of hard work and a steep learning curve I have completed my first gem! w00t! You can download it if you want:

$gem install blam_and_woot

If you want to peek at the source code you can find ithere

I will be writing about packaging the gem and some issues I ran into with that later. For now, I am celebrating!

Sunday’s Challenge - Learn With Jeff

Full of the knowledge gained from Coder Night on Thursday, I made an attempt at creating a poker game. My original plan was to create three classes, a Player class, a Deck class and a Game class. The general construction of the game would be the Game class would use take a new deck object and pop the last card out of the shuffled array and assign it to one of the players in the array “hand”

The first problem I ran into was getting each player cards from the same deck. When I wrote the program the first time I assigned a new deck to each player.

I think I finally figured out the problem there, but I broke the program :/

I am pretty sure the problem is somewhere in my Game class. Any ideas?

require_relative 'player'
class Game
  attr_reader :hand
  attr_accessor :make_game
  def initialize numplayers
    @numplayers = numplayers
    hand = []
  end
  def make_game
    i = 0
    while i < @numplayers
    hand << Player.new()
    i+=1
    end
  end
end

Yeah… I’m a Bad Blogger. - Learn With Jeff

I’m coming back now, I promise

I’m sorry, audience of zero, I haven’t been updating you on my learning process or helping you keep up. The problem is, I have been moving way too fast to write about it every day. So, to update you:

HTML:

While I am no master, I plowed through my HTML book and felt comfortable enough to move on.

CSS:

I know the concepts of CSS, I know what it can do and I know where to look up commands, I think I will get better by doing.

So the blog originally was for you to learn with me and I dropped the ball on keeping up with it. Sorry.

So, Where am I now?

JavaScript

I have been playing around with codecademy for a few months now and I love it. The exercises are challenging and you have a lot of freedom to mess around. It was a great intro to programming and I have since expanded my pursuits!

Ruby

Through the urging of my wonderful friends Jessica Barnett and Aubrey Goodman, I decided to pursue Ruby. I love it. Actually love is a gross understatement. When I sleep, I have dreams of objects and methods dancing in my head (you think I’m joking).

I am currently working my way through the Learn to Program by Chris Pine (highly recommend) and a course offered by Pragmatic Studios (equally awesome).

Needless to say, I don’t want to do anything else but program in Ruby for the foreseeable future.

General Computer Science

For this I am reading Schaum’s Outline which moves really fast but is completely worth the effort. I think this will prepare me well for some further exploration into algorithms and databases.

Java

I am also going through Standford’s Intro to Programming Methodology and Computer Science class to hopefully give me some training in larger theories and best practices of software design.

So, yes I am busy. I will however try to post more often with some tidbits, challenges and advice I pick up along the way. I hope you continue to read!

Setting Up Your Website - Learn With Jeff

Homework Answers:

<html> and </html> let the browser know where the html document begins and ends

<head> and </head> starts and ends the page “head”

<title> and </title> Gives the page a title

<body> and </body> starts and ends the “body” of the document.

<h1> and </h1> changes the formatting of the enclosed text to the size of  heading 1

<p> and </p> shows when a paragraph begins and ends

<h2> and </h2> Same as heading 1 except slightly smaller.  There is a total of 6 heading sizes

Today’s Lesson

Setting up your website

In order to begin creating your website you have to first choose a text editor. If you have a Mac you can use TextEdit. If you have a PC, Notepad comes standard. Thanks to Jessica I will be using TextMate which is a text editor made specially for developers.

So open up the text editor of your choice and make sure that it is set to a plain text format (not necessary if using TextMate)


Save the file as “index.html” to a folder called “learnwithjeff.”
Now, try creating a file like the one I did in my previous post. Once you have finished open the file using your browser and check out your first HTML website!

 

Lesson 2 an Intro to HTML - Learn With Jeff

Disclaimer:

Third post in and I’ve already made an executive decision about this blog, it is about me. I know this may sound a bit selfish. I am okay with that.

I want you to read, follow along and learn with me. I will answer all questions as best I can. I won’t, however, choose what I teach based on popular demand. I want to learn, so I won’t cover things I already know.

So, on that note, we are not going to go over WordPress today like I had originally planned. We are going to go straight into HTML.

Now I know a little HTML but I figure it can’t hurt to get a little refresher so I will start from the beginning.

HTML:

I got my book in the mail, Head First HTML with CSS & XHTML. It looks pretty basic and seems like a lot of fun (Lots of pictures). I will try to explain everything as best I can but I will always refer back to the book. If you want to follow along I would suggest purchasing it.

So let us first delve into the basics of HyperText Markup Language or HTML for short. To do that we need to first take a look at where it fits into the equation of the internet.

HTML is the language your browser understands. When you make a website you load HTML file(s) onto a web server. A web server is just a computer that stays on, connected to the internet.

When you visit a website you are sending a request to the server to send you that HTML file. Your browser than interprets all the HTML tags (certain words surrounded by <…>), and displays websites as you know them.

 


So now we know where HTML fits into the web in general. It is a language that browsers use to determine the structure of a website. Next lesson, basic HTML tags.

See you next time :)

For My First Lesson My Digital Resume - Learn With Jeff

I’ve done a lot of cool things in my short 24 years. I’m proud of my accomplishments and I want to show them off to future employers, partners and investors. To do this, I am making an online resume. First, I need a url. To do this you need to find an available domain name and a domain registration company. Here are some of the tools I used:
  • Domai.nr - This tool helps you find interesting domain names
 
  • Fat Cow - I love this website. It handles my domain registration as well as my hosting. It has one click Word Press installation, unlimited storage and a free domain name. It may not be the best in the market but I have been very happy with them.
 
Some other tools:
  • Domain Registrars
  1. Name Cheap - Voted number one by lifehacker
  2. 1&1 - Came highly recommended
  3. Godaddy - I have used in the past, generally more expensive and not user friendly
  • Website Hosting
  1. Blue Host - Highly recommended by friends
  2. Host Monster - Generally good online reviews
  3. Godaddy - Popular but again, more expensive and not user friendly
What I did:
  1. Searched for domains using domia.nr. I tried a few combinations and settled on hirejeffbaird.com
  2. Opened a new account of Fat Cow, entered in my desired domain name and credit card information. I am now the proud owner of hirejeffbaird.com and have a hosting account to go along with it. Yay!
Tomorrow’s Lesson: Getting Started with Word Press