Hi. I’m Theo Vora.

Klutzr, Stumblr, Developr

git

git stash Basics

Do you ever see this?

1
2
3
4
5
error: Your local changes to the following files would be overwritten by checkout:
  file.txt
  dir/file2.txt
Please, commit your changes or stash them before you can switch branches.
Aborting

This occurs when you run git pull, and your current branch has changes. git aborts the pull, because doing so would overwrite the changes in your current branch.

So what do you do?

The work you’ve done in file.txt and file2.txt, is important, and you don’t want to lose them.

git stash is a great way to save your work without running git commit.

NOTE: git stash is not meant to replace git commit. When you have stable changes you’ve made to your code, you should use git commit.

I’ve found git stash particularly useful when my team has pushed important updates in to the master branch when I was in the middle of something. Normally, I would git checkout master then git pull. But since I have made some changes that may or may not be fully functional, I run git stash to save my current progress so that I don’t introduce and unstable commit.

Basic Stash

1
git stash

This will stash all files that are unstaged or untracked.

Where does it go exactly?

List your stashes

1
git stash list

Unsurprisingly, this will show you a list of all of your stashes.

Note that I said “stashes.” So you can have multiple stashes? The answer is yes!

The next time you run git stash, that stash will pushed to the top of your git stash stack.

Looking inside a stash

Time passes. You forgot what you stashed. git stash list only shows you a list of stashes. What if you want to look inside of a stash?

1
git stash show -p stash@{0}

The last argument stash@{0} represents the name of the stash. This comes from git list. The -p option will run git diff on each file that is in the stash.

Retrieve a Stash

Ok, so you’ve stashed your important files. Now it’s time to get them back.

There’s more than one way to retrieve a stash. Let’s start with the simple one.

Apply

1
git stash apply stash@{0}

Again, stash@{0} is the name of the stash you want to apply. You can get it from the git list.

Pop

1
git stash pop stash@{0}

pop works just like apply. The only difference is it also drops (aka deletes) the stash from the stack.

Given the frequency with which I use git stash, I prefer pop over apply, because your git stash list will get very longer over time. Wouldn’t it suck to run git stash show every time I want to remind myself of a stashes contents? I would much prefer to find my stash from a smaller list than a bigger one.

When I pop, I’m pretty sure the files I work on will eventually lead to a commit. If not, I can always git stash again!

Choosing what files to stash

Ok, so git stash is great when you want to stash everything, but what if you want to stash just one file? What about a handful of files? I’m going to show you 2 different ways.

Stash with -patch

1
git stash -p

If you run git stash -p, will run in an interactive mode. Like a software wizard, git stash -p will show you an unstaged file (showing its git diff) and then ask you if you want to stash this file. This process repeats for all unstaged files.

The -p is short for -patch. I have no why it was named this way. But I find it easier to remember -p if I think of the word “picky.”

Stash what’s not staged

Another approach is to stage the files you don’t want to stash. Then stash everything else.

First, git add the files you want to stage for the commit.

1
2
git add file_you_want_to_commit.txt
git add file_you_want_to_commit2.txt

Then, git stash --keep-index. All unstaged files will get stashed. --keep-index is basically saying, “do not stash my staged files.”


Sublime Text Demo: How the f did they do that? Part 2

If you were blown away by the 6 demonstrations on the Sublime Text 2 website like I was, you were probably wondering, “how did they do that?”

I covered how to do the first 3 demos in detail in a previous blog entry. Check out part 1.

In this blog, I cover the last 3.

4. Goto Anything – a file

Keyboard Shortcut

Mac: Command P

Win/Linux: Ctrl P

Order of Events

  1. Press ⌘P
  2. Type loa. These are the first 3 letters of ‘loader.py’ which Sublime Text returns to you.
  3. Press Enter. ‘loader.py’ opens.
  4. Press ⌘P
  5. Type hutil. Notice that the file ‘utils.py’ from the http folder is returned. Apparently, django has more than 1 ‘utils.py’. There’s another ‘utils.py’ in the ‘db’ directory. What would you type to get the db file? ;)

Why you would use this

Have you ever had to “drill” into directory just to open up one file? By “drilling”, I mean you open subdirectory after subdirectory until you find the file you want to open. Sometimes, my initial drill doesn’t even yield the file for which I’m looking! I have to drill somewhere else!

This is a huge timesaver. Sublime Text 2 eliminates the need for you to remember the exact location of a particular file. Just press this shortcut, type any 3 letters of the file you want (it could even be con of ‘application_controller.rb’), use the up and down arrows to change your selection, then press enter to open your selection.

5. Goto Anything – within a file

Keyboard Shortcut

Mac: Command P

Win/Linux: Ctrl P

Syntax

1
2
3
4
# syntax: file_name@keyword

# example: loa@ftl
# result: will show you `def find_template_loader` inside of loader.py

Order of Events

  1. Press ⌘P. The Goto Anything pane appears.
  2. Type loa. Sublime Text highlights ‘loader.py’. Notice you can see the contents of the file that’s currently selected in the text editing pane.
  3. Type @. At this point in time, the Goto Anything pane shows the list of functions inside of ‘loader.py’.
  4. Type ftl. Sublime will highlight the function named def find_template_loader.
  5. Press Enter to see this function highlighted in the text editor.

Why you would use this

During development, you’re always running into errors. The errors are usually helpful in telling you where the problem occurred – the file name is given and sometimes with a line number or function/method name.

If the file in question is not open, you can easily open it with ⌘P and add tack on @ along with your function/method name to jump to it quickly.

6. Find and Replace with regular expressions

Keyboard Shortcut

Mac: Command F

Win/Linux: Ctrl F

Order of Events

  1. Press ⌘F. The find pane appears at the bottom of the Sublime Text window.
  2. If it’s not already selected, press the Regular expression button. It’s the left-most button in the Find pane.
  3. Type “ +” (yes, there’s a space before the plus sign). This highlights all occurrences of one or more spaces.
  4. Press Find All. This selects all occurrences of one or more spaces.
  5. Press right arrow (→) once.
  6. Press Backspace once. (Macs press Delete)
  7. Press Esc to exit multi-cursor mode.

Why you would use this

Could you imagine changing all of the double spaces to single space in that document manually?

Granted, you didn’t need a regular expression to solve that particular example. You could have just done a normal Find All (with the regular expression button turned off) on double spaces.

Sooner or later, as a programmer, you will have to reformat a large body of text. Regular expressions allow us to find patterns of text. I’m not prepared to give a talk on regular expressions in this blog, but it’s definitely useful to understand how they work (I’ve listed a few tutorials below). It’s awesome that Sublime Text 2 allows you to toggle regular expressions in your search if you choose.

Here are a couple of tutorials on regular expressions. I haven’t completed them myself, as I learned them in school a long time ago. In the interest of being helpful, I liked these 2.

http://regexone.com/

http://regex.learncodethehardway.org/book/


Sublime Text Demo: How the f did they do that?

Update 11/5/2013: Cleaned up formatting. Note to self: Markdown tables are not reliable. Also, check out part 2!

If you have seen the demonstration on Sublime Text 2, you might be asking…

How the f*** did that do that??

There are 6 demonstrations in all, and I’ll break down the first 3 for you.

1. Multiple Selection

Keyboard Shortcut

Mac: Command D

Win/Linux: Ctrl D

Order of Events

  1. Double-click a variable name (len in the demo). This selects/highlights the variable.
  2. Press ⌘D for each occurence of the variable.
  3. Type away! (in the demo they typed length)
  4. When you’re done, press Esc

Why you would use this

As programmers, we’re always changing our variable names. The same variable could appear 2 times or maybe 20 times (or more) in the same file! Whatever the number, this handy shortcut will save you time in the renaming process.

2. Split into Lines

Keyboard Shortcut

Mac: Command Shift L

Win/Linux: Ctrl Shift L

Order of Events

  1. Write the days of the week on separate lines. See sample below.
  2. Select all the text. Use either your mouse or use ⌘A.
  3. Press ⌘ ⇧ L. This will split your selection into seperate lines.
  4. Press “ once. This will put quotes around each day of the week.
  5. On Mac, you can use ⌘→ to jump to the end of the line. Win/Linux can use their End key.
  6. Press , once. This will place commas at the end of every line.
  7. Mac users, press Fn Delete. This brings the days into one line. Win/Linux can use their Delete key. You want the key stroke that allows you to delete the character on the right side of the cursor.
  8. On Mac, press ⌘→. Win/Linux press End key.
  9. On Mac, press Delete to delete the last comma. Win/Linux use backspace.
  10. On Mac, press ⌘ ⇧ ← to select everything on this line. Win/Linux can use Shift Home.
  11. Press [ once. This will enclose your selection in square brackets.
  12. On Mac, press ⌘← to move the cursor to the beginning of the line. Win/Linux press Home.
  13. Type days =
  14. Done!
1
2
3
4
5
6
Mon
Tue
Wed
Thu
Fri
Sat

Why you would use this

I don’t think this use case is used in everyday programming, but the little steps between them are super powerful!

The name “Split into Lines” is a little misleading. I would call it “whatever contiguous block of text I selected, turn it into individual line selections.”

Inside of a multi-line selection, you can jump to the front or the end of each of those lines. You can even move with just left and right arrow. Play with it and be merry!

3. Command Palette

Keyboard Shortcut

Mac: Command Shift P

Win/Linux: Ctrl Shift P

Order of events

  1. Press ⌘ ⇧ P
  2. Type sspy. This will tell Sublime that the syntax is Python. Then you see syntax highlighting appear.
  3. Press enter.

Why you would use this

Ok, so you can set syntax. So what?

By setting syntax in Sublime, you not only get syntax color highlighting, but you also get access to the snippets of that language. I am known to set syntax of .erb files to HTML5 just to take advantage of the HTML5 snippets.

This leads me to another point! HTML5 snippets are not a standard part of Sublime. If you have Package Control installed, you can invoke it from the Command Palette! Type in package control to see all avaiable commands.


Name Game Takeaways

After 3 weeks at the Flatiron School, my teammates and I decided to embark on a mini project. We had dabbled in a lot of Ruby, a little sqlite3, and a little Nokogiri. We felt the time was right to apply what we learned and deploy something small.

Play Now

http://theevo.github.io/name_game/

Source Code

https://github.com/theevo/name_game

Authors

https://github.com/manleyhimself

https://github.com/PHJ45

https://github.com/theevo

Takeaways

This is the first Ruby project that I’ve ever carried out from code to deployment. I have to thank my teammates. We were able to fill each others gaps, and I’m positive I’d be stuck on writing the class if I were doing this alone.

I could write a long ridiculous monologue about how we did it from start to finish, but you’d probably get bored. Instead, I’ll just share a 3 interesting discoveries I learned while working on this project.

1. Grabbing Random Images

Instead of having to pull random images and then having to decide this in Ruby, we found a way to make sqlite3 do it for us.

sqlite3 RANDOM()
1
SELECT image FROM students WHERE id != ? ORDER BY RANDOM() LIMIT 2

Note: RANDOM() doesn’t work in MySQL. It’s RAND(). So check your flavor of SQL’s documentation first.

MySQL Rand, not Random
1
SELECT image FROM table1, table2 WHERE id != ? ORDER BY RAND() LIMIT 2;

2. Handling Special Characters

We ran into an issue with one student’s name where the student’s name carried an accented letter “o” in it. In order for this character to display correctly, we had to add the meta tag for UTF-8.

1
<meta charset="utf-8">

How did this happen in the first place? In our rush to build this app, we used the Sublime Text 2 HTML snippet, which does not include this meta tag.

To prevent this issue from occurring in the future, I have installed a different package of HTML snippets. This one includes the UTF-8 meta tag, and it also includes the HTML5 doctype. I’ll be HTML5-ready from the get go! :)

http://joshnh.com/2012/11/27/a-set-of-html-snippets-for-sublime-text-2/

3. Deploying our Game

When it came to hosting our app, I immediately thought of Github for some reason.

It turns out that there are 2 kinds of web pages that Github will host for free:

  1. User/Organization Pages. All of us are familiar with this one thanks to Octopress.
  2. Project Pages.

Here’s the low-down on getting your Project Page hosted by Github. All you need to do is create a new branch in your repo called gh-pages, push it, and GitHub will serve it up.

And yes, you can have both simultaneously. :)

Here’s an example:

Your repo: http://github.com/your_github_name/project_name (branch: gh-pages)

Where it’s hosted: http://your_github_name.github.io/project_name

References

MySQL – Function Rand

http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand

http://davidwalsh.name/mysql-random

Github Pages – Creating Project Pages Manually

https://help.github.com/articles/creating-project-pages-manually


Ruby Blocks 101

For the longest time, yield had my mind spinning in circles. The teachers here at Flatiron have provided us with many articles on the topic, and I’ve even done my own research to really try to get a handle on it.

You could just watch this instead

If you’re like me and struggling with Ruby blocks, I recommend this slideshow created by Jonathan Cairns.

What I like about Jonathan’s slideshow: it reads exactly like a tutorial. He starts out with some simple examples and then ramps it up. Best of all, he’ll show you some code, you can think about the answer, then he shows you the answer when you navigate forward. BRILLIANT.

In this post, I’ll attempt to share what I understand about Ruby blocks and yield.

WTB: What the block?

A block is simply one or more lines of code. Let’s look at some Ruby examples.

A one-line block is bound by curly braces { }.

1
2
3
# Example 1: a one-liner block

5.times { puts "hello world" }

For longer blocks, they’re bound by the keywords do and end.

1
2
3
4
5
6
# Example 2: a multi-line block

5.times do
  puts "hello world"
  puts "goodnight y'all"
end

The following is functionally equivalent to the first example, but convention dictates that one-liners should be enclosed with curly braces { } as demonstrated in Example 1.

1
2
3
4
5
# Example 3: a one-liner written with do-end

5.times do
  puts "hello world"
end

Passing the block

Let’s start with some examples. They do the same exact thing. The only difference is the first does it by accepting an argument, the other with a block.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Example 4: say_3_times method with arguments

def say_3_times(string)  # method
  puts string
  puts string
  puts string
end

say_3_times("Let it be")  # the caller

# OUTPUT:
Let it be
Let it be
Let it be
1
2
3
4
5
6
7
8
9
# Example 5: say_3_times method with block

def say_3_times  #method
  yield
  yield
  yield
end

say_3_times { puts "Let it be" }  # the caller

Order of execution:

  1. say_3_times is called
  2. we enter the method definition def say_3_times
  3. the 1st yield is called
  4. we enter the block of the caller
  5. puts is executed once
  6. we return to next line of def say_3_times
  7. the 2nd yield is called
  8. we enter the block of the caller
  9. puts is executed once
  10. we return to next line of def say_3_times
  11. the 3rd yield is called
  12. we enter the block of the caller
  13. puts is executed once
  14. we return to next line of def say_3_times
  15. we reach end of method definition def say_3_times
  16. done!

What it means to yield

Based on the order of execution, we can make the following conclusion on yield’s behavior.

yield will execute the block of the caller

Why would you ever use yield?

When I first saw yield demonstrated, I thought, “Well, that’s nice. When would I ever use it?”

Turns out that it’s used everywhere by Ruby experts. It’s used in your BFF array method: .each.

I’ll expand on practical use of yield in future posts. Stay tuned.

Additional References

Definition of block according to _why http://mislav.uniqpath.com/poignant-guide/book/chapter-3.html


First, Make It Run

Early on in my career as a budding programmer, I would write pages and pages of code before ever trying it out (aka compiling or running).

This was totally wrong. I wish I could tell my younger self what I now know is the right way to approach any program.

Avi, our head instructor at Flatiron School, has repeated this mantra to us:

“Make it run, make it right, make it fast.”

Order of Events

First, you should make your program run. After that, you can refactor it using built-in, human-readable Ruby methods like .each and .reject. Then once all of that refactored code works, find ways to make it run faster.

Experts always tell us that the first thing you should do when approaching any complex task is to break it down into smaller, manageable pieces.

Avi would probably add this one piece of advice to that (forgive me for paraphrasing this, Avi):

What’s the simplest piece of code we can write? Let’s write that first. We need some immediate feedback from the computer.

Practice what we preach

On day 8, our class was presented with what seemed like an impossible challenge at the time:

  1. Parse the Reddit API
  2. Filter out only SFW (Suitable For Work) posts
  3. Output a new HTML page with the title, link, picture (if one is available), up votes, down votes

The Reddit API is monstrous piece of data. At this stage in our work, here’s what we should/shouldn’t be thinking:

Yes, Think This

  • .json to hash
  • print hash to stdout
  • ask the hash questions like .keys .values

No, Forget That

  • ensuring we use the best possible hash converter
  • optimizing for HTML5
  • iterate through hash (save that for later!)

That last part “iterate through hash” is important. Why do we need to forget it (and save it for later)?

Answer: Did you see that API? We don’t even know what we’re going to iterate on yet! First, let’s figure out where our desired data lives. Our iteration will likely take place in that data’s parent (or maybe a little higher).

Our team’s result, while the work of Ruby amateurs, nonetheless runs and meets the requirements.


hello world

Oh my gosh, I figured out how Octopress works!