Ruby, etc

Learn all the things

He who enjoys doing and enjoys what he has done is happy. - Fortune Cookie

Trying Out Emberjs Models and Views

I’ve read about emberjs and I’ve tried CodeSchool Backbone and the concepts are interesting, I like the concept of a model updating some text on the screen without having to write $(‘#myelement’).text(‘updated content’) and a friend was mentioning how much they like the emberjs models. So, I wanted to try and in a way I could only worry about a few pieces (that router and junk still kinda makes me crazy for frontend since I do that on the back end.. maybe I just need to do more). I was reading the Ember Documentation and they have some small examples that inspired me. So I set out to try somethings:

I downloaded the Starter Kit from emberjs.com and it had a skeleton index and app.js file ready to go. Nice. I first played around with making a model and calling a method on it.

Hello World

Trying out octopress

Trying out
1
2
3
colors = %w(red blue green)
colors.include?('blue') # should be true
colors.include?('yellow') # should be false

I started with jekyll and that was cool. a little more manual, then I discoverd octopress and I only had to do a few things to change the posts I’ve converted (basically change code tags). I am going to stick with this one.

Thanks to Dan Watson for the category_tag plugin!

Digging Through Sludge

Or how to dive into a large untested project and be productive

This is how I feel sometimes when I come upon a large untested project:

I’ve been talking to folks and pondering this question..how to be effective (in a new to me) large untested project? I was listening to RubyRogues and they mentioned this talk at Cascadia Ruby (I immedately recognize the name as one of the devchix and they praised her talk Therapeutic Refactoring. I’ve listened to it twice so far. Its kind of entertaining too!

Evan Light mentioned this book Working Effectively with Legacy Code … I think I flipped through that once in 2008, the java examples made my eyes glaze over, but I should give it another chance. He gave a talk at (Lone Star Ruby Conference)[http://lonestarrubyconf.com] and refactored a bit of code by breaking it into chunks. His (slides)[http://elight.github.com/fdd/]. He didn’t cover tests, but it shows practical ways to get started.

I finally checked out www.destroyallsoftware.com also mentioned on that episode of RubyRogues and he has a series of screencasets on adding tests and refactoring a codebase. Start with this episode.

Finally, some practical techniques for diving into a codebase and starting to write tests. I think I have enough tips to get started as I dive into some code this week.

Screen Multiplexing == Productivity

A few friends have been telling me about tmux and since I’ve been using a mac full time again (rather than ssh into linux) I haven’t used a screen multiplexer. I wrote awhile back about Getting around with GNU Screen and I really liked screen. I was listening to Changelog’s podcast Episode 0.7.3 - Tmux with Brian Hogan and Josh Clayton on one of my walks, it seemed useful and better than screen. Then I remembered hey it was kind of useful to use a screen multiplexer (BTW, I love saying multiplexer. I feel smart, like I know what I am talking about, hehe). I got home from my walk and installed it and started with the links on the changelog show notes and not wanting to get overwhemed by just copying someone conf file, I started simple and added a few things:

Adding an Application Config File to a Rails Project

Recently I worked on a project where I needed certain config variables for staging, production, test etc and I wanted to store all these in a yaml file. I could set them in the different environments files but I prefer them all in one file.

Create a file in the config/initializers .. I called it 00_load_app_config.rb so I can be sure it gets loaded first. In it I have only a single line

Practicing Ruby With RailsCasts

I recently went on a small vacation and finally had some time to catch up on some RailsCasts! I like to setup a project and “play along” with Ryan Bates and pause/play and work through the examples. With all my experimentation/playing I keep a git repo at http://github.com/rubygeek/rubygeek where I keep code for reference later. Sometimes when trying to remember if i did something, I grep a keyword or two on the root directory and see if I have done something with it before. I can put notes in the readme file for each project, so I can remember what resources I used.

Guard Setups up monitoring for files to run something like tests, compile coffee script or sass. The cast shows how to use it to run your rspec tests. It shows liverefresh, with that and a plugin it will automatically refresh your browser!

Ancestory I spent a good deal of time using acts_as_commentable_with_threading and although its pretty good, I think now I will change to using this, it seems like it will be more efficent on queries and has more scopes defined.

Creating a Image Gallery in jQuery

For one of my projects, I wanted to have a link that will open an image gallery in a lightbox. I want thumbnails at the bottom, that would display above the thumbs but larger. I found alot…alot of them in jquery. I found blogposts that review 30 (and and 10 and and 15 and and 14 and there is more!) of them at a time! So, do I try and modify one.. or .. do I write one myself?

I was also inspired by Garann (fellow DevChix) because she decided to write her own text editor (something I probably wouldn’t attempt…but she said it was not too bad! so maybe it wouldn’t be that bad after all??)

So simple interface:

Clicking the link will open the gallery, looking like this:

Koan-A-Copia!

I first learned about Koans from EdgeCase Ruby Koans project, it was great fun and great practice. Since then, whenever I mentored a DevChix or RailsMentors member I suggest to start doing Koans… even on a regular basis, once you finish them all … then start over! I always have a ruby koan mid-way and work on it when I want to practice ruby. Sometimes I try to complete the whole file then run it. Since I’ve been learning Groovy this week, I looked to see if there was Groovy Koans, found those.. then I just started compiling all the languages I know of..

Ruby

http://www.rubykoans.com

Python

https://github.com/gregmalcolm/python_koans

Getting Around With GNU Screen

In past few months, I worked on a project developed entirely on linux. Previously, I mostly did rails development on mac with textmate. I had a brief period of rails and perl where I did vim and screen…oh maybe 4 years ago. Anyways, so I didn’t forget much vim over the years, but I had forgotten how to use screen. I looked some documentation to refresh my memory and this post is mainly notes for me :)

Most commands start with CTRL-a I refer to each spawning of a new screen in the current session as a window.

Detach

One things i really like about screen is I can detach it on one computer… then log in somewhere else and reattach. Also handy when you are on a wifi card on the train and you get disconnected. (doh!)

1
ctrl-a d   to detach

then start with -r to reattach:

1
 screen -r

Create window

This creates a new terminal window

1
ctrl-a c

Name that window

Name your window, so its easier to keep organized

1
ctrl-a A (Yep, ctrl-a then SHIFT-a)

List the windows

See a list of your sessions and their number (this is why you name them) and you can use arrows to select)

1
ctrl-a "

Navigating Windows

You can flip through the windows in order or specify the number:

1
2
3
ctrl-a p   #previous window
ctrl-a n, ctrl-a [spacebar]  #next window
ctrl-a #   #the number you want to go to (starts at 0). 

multiple regions in one

1
2
3
4
5
ctrl-a S   #create a split, creates a new region
ctrl-a TAB #switch to next region
ctrl-a c   #create a terminal session in region 
ctrl-a X   #close the region
ctrl-a C   #clear, this is like typing clear at the prompt to clear the screen

Closing

To exit a window, simply type exit. To exit and kill all windows do

1
ctrl-a ctrl-\

Scrolling

Using terminal on mac or linux won’t capture the scroll back…. so you must do it through screen

1
ctrl-a [    #use the arrow key to navigate up 

Refresh

1
ctrl-a l  #refresh the current display

HELP!!

To see the two (!!) pages of screen commands type:

1
ctrl-a ?

Command mode

Do you like typing?

1
ctrl-a :   #to get to command mode, then you can type commands instead of the ctrl foo jibberish

Need the time?

1
ctrl-a t   #displays the current system time

Named Screen Sessions

Maybe you are working on two separate projects at once, give each one its own screen session

1
2
3
screen -S ProjectOne
screen -S ProjectTwo
screen -list

then later you can do

1
screen -r ProjectOne

to reattach it and continue

One thing you can do it make it easier is to add this to your .screenrc

1
hardstatus alwayslastline "%?%{yk}%-Lw%?%{wb}%n*%f %t%?(%u)%?%?%{yk}%+Lw%?"

It will show the names of the windows you have and highlight the current one. You can see the numbers too so you can do ctrl-a # quickly to jump around.

Anyways, hope this was useful to someone. Let me know any suggestions or anything I can do better!

Sources: * Unix Screen * Screen Man Page

Ruby-Debug

I never really used a debugger until I did Java. Before that I did what was called “PHP Debugging” which is littering your code with print statements until you figure out what you need to know, then going back and making sure you deleted them! In php I sometimes would put a unique string like

print “QWE” . $myawesomevar

Or something like that, then going back and searching your code for QWE (just picked something that was easy to type!). But all that gets rather annoying. They might have nice debuggers for PHP now, I haven’t really done php in 4-5 years. I really didn’t understand the power of breakpoints until I did java and I thought that was pretty cool. Now that I do all ruby, I sort of miss eclipse (I didn’t actually think I’d ever say that…) since all I use right now is textmate and vi which don’t have the built debugger support that eclipse does. Some of the newer ruby editors might, I haven’t spent any research on them.

I found ruby-debug and it works great! You can set a breakpoint and then climb inside your application and peek around and see what is going on. I’ve done this in controllers and even in test mode.

If you use passenger, you can still use it but its a little tricky to setup. This blog post from duckpunching.com has some neat tips and a rake task to make it easier. Be sure to start the rdebug -c from the console after you have set a breakpoint and reloaded your page.

If you are using passenger, you can’t use the irb command inside of the ruby-debug console, but I found that most of the time just being able to poke around the variable space inside my app has been enough for me to understand what is going on.

There are quite a few commands available in the debug console, but these are the ones I’ve used:

  • help - view the list of commands
  • l list - this shows context around the current line the debugger is on
  • n next - moves to the next line
  • p print - print a variable
  • irb jump to console - I dont use this very often and it doesn’t work if you are using passenger
  • c cont - continue to end of breakpoint or end of page load, after I have seen what I want to see I do this to get out of the console

Resources

Anyways, hope some of you find it useful and happy debugging :)

Update: I wrote this in preparation for a short talk at AustinOnRails while giving the talk, the group had some suggestions:

put in a ~/.rdebugrc

1
2
set autoeval
set autolist

autolist - will run list command when you enter autoeval - will eval each line, so instead of “eval @profile” to get it to print, it will automatically eval

doing m [object] can show methods in a nice table format. Thats cool, i have often gone to irb and did String.methods.sort but that is kinda hard to read.

Anyways, good stuff!