18 Feb 2013

Toggling Pry in Ruby Script

I’ve been using the daylights out of Pry ever since learning about it ~ 1 year ago. I’ve also been evangelizing about it to anyone who will listen:

@_zph Thanks for reading!And thanks for teaching me tmux and pry.Been trying them here and there - still very shaky with tmux.

— Steve Jackson (@stevejxsn) February 16, 2013

The most recent way that I’m using Pry is by dropping the following line into anywhere that I need to work with:require 'pry'; binding.pry

But adding that to scripts and then deleting it becomes repetitive. So here’s a little Vim function that that toggles my Pry statement. If the current line matches the Pry snipped, the line’s deleted. If the current line doesn’t match the snippet, then the snippet is added at the cursor. ( It’s best used on a blank line until I make the function more robust. )

Special thanks go to @Banisterfiend for all his hard work on the Pry gem. It makes my Ruby life a much happier experience. If you’ve benefited from his hard work, please consider a donation @ the following link.

Also, special thanks to Tim Pope. Reading his vim-fugitive code gave me the syntax hint on how to make this work.

17 Feb 2013

The Value of Experienced Developers

I had the pleasure of talking today via Twitter and Skype with Wes Garrison, Founder of Databasically, one of the organizers of Ruby Midwest and all around nice guy. We met back at SteelCityRubyConf during the CodeRetreat and have been in intermittent Twitter contact since.

We talked about a project that I’m finalizing in order to pitch to my company. His feedback on the app, especially in pointing me directly to a solution for highlighting, reminded me that I need to spend more time with experienced developers. In those few minutes he pointed me to the tools that increased the value and utility of my product. Within minutes of getting off of Skype, I added the highlighting feature, tweaked the CSS, and a bit of the underlying Ruby. It’s a big improvement :).

After the Skype conversation ended, the conversation continued on Twitter with Wes pointing out that I should consider using the Decorator pattern in my Rails application. I remembered seeing Draper on Github and an hour later I had a working implementation that abstracted away some complexity from my views. Yet another point related to him having more experience than I.

I’m reminded how much easier it is to learn from people who have already walked a certain path and distilled the necessary knowledge, rather than re-inventing the wheel yourself. This isn’t to say that developers should rely on others for all their answers, but I’ve been 100% self sufficient with my trusty Google-based answers thus far. It’s time for me to better network and share knowledge.

To this end, I need to make time for remote pair programming. I met @JoelByler of LeanDog at a CodeRetreat in Cleveland earlier this year. We discussed remote pair programming for fun, but with household responsibilities, preparing a product to pitch, and longer than normal work hours it’s been tough to schedule.

But tough to schedule is no excuse. I’m getting more diligent about reducing my time consuming blog posts and external information and making a dedicated goal of increasing my productivity. Not hoaky productivity like you see blogged about on Lifehacker.com where you read about a million ways to improve productivity while accomplishing nothing in life. I’m talking serious productivity of cutting out unnecessary fluff time, idle web browsing, and conserving effort for producing things. Some of my current goals in this regard are another couple of web applications related to my industry, a series of webcasts, building a newsletter list of people interested in the theme of the screencasts and providing that set of readers with tailored articles. I’ll have a much easier time of this if my employer sees the value in the product I created and transitions me into a development role. That way the web apps can be developed during work time :) where they will also be consumed by my fellow teammates.

In closing, I’m working on things that I can control and appreciating good advice when I get it.

If you’re reading this in the Kansas City area and need help with tech stuff, Wes Garrison @ Databasically will solve your problems. Give him a ring.

If you’re in the Cleveland area or just want a good group of people to work with for your Rails application, get a hold of LeanDog. Joel Byler, Steve Jackson and the rest of the crew are a good group of folks.

16 Feb 2013

Ignoring All the Things

I found a great talk by Scott Hanselman on Vimeo about information overload and learning to ignore things. In the video he laments that more conversation is taking place in walled gardens (ie Twitter) rather than on the wide open web (ie blogs). He also summarizes what I’ve felt for a few months: * that keeping up with news and the latest things is reducing my ability to produce and innovate *.

It comes back to the concepts in this article: 500 Words before 8am. Start the day as a producer, not a consumer.

In order to work on this, I’ve set myself up with RescueTime and am tracking the amount of time spent on ‘consumptive tasks’ (no relation to romantic portrayals of consumption by Val Kilmer in ‘Tombstone’). I’m receiving pop-up warnings when I exceed a set quantity of time on reading Twitter, reading blogs, etc. I’m also looking to transition more of my consumption time to ‘curated content’ such as ‘ye old books’.

Another point that I took to heart from Scott Hanselman’s talk was about learning to use IFTTT. It’s an SF startup that makes common web tasks automatic. Think of it as a recurrent cron job for the web. Things like : ‘anytime I start a tweet, add the contained links to instapaper’, or ‘anytime I start a vimeo video, post it to twitter’. One filter that I added to help with the Ruby Rogues Parley list is ‘anytime I star a gmail message, copy the content into Evernote’… this should help easily archive good content for later viewing and recall :).

Well, that’s it for this round of production. I spent much of last night working on an internal web application for work that I plan to soon pitch to my company :). I finished the final polish on it (CSS, a little Javascript) and I must say that it feels 100% better. It’s a good reminder for me about how important the UI is for applications.

14 Feb 2013

Getting Started Configuring Bash/Zsh

I saw a post this afternoon by a member of Ruby Rogue’s Parley list asking how to write Ruby code to help them create a shorthand word for a longer commandline command. It takes moxy to ask questions on a list filled with many experienced developers. I was happy to see that positive responses flowed in. The immediate response was that the individual should perform the action by writing commandline aliases using the shell ‘alias’ command. But that only points the developer in the right direction. Instead of just pointing in the direction, the rest of this post will be walking a new developer through setting up helpful customizations in the shell environment. (Note, the generic information will be fairly compatible whether using the BASH environment or ZShell environment. Moving past general configurations into advanced configurations are where the two start to diverge, particularly with the environmental settings used in .bashrc or .zshrc files. I’ve found that the best remedy for this is reading through and gradually adopting a few lines here and there from well commented .bashrc/.zshrc files). Many of the customizations in the shell relate to minimizing repetitive keystrokes. Imagine the difference between typing

bundle exec rails server

and

be r s

If you’re typing this more than occasionally, it’s a large savings of keystrokes over a period of months. So how is it done? Aliases! Simply put, include the following code in your config file (a generic term that I’ll use for either .bashrc or .zshrc, depending on your shell): alias j="jobs" Or to help avoid dumb mistakes (it makes the remove command verbose and interactive =D. Even astute geeks fat finger the occasional keys ) alias rm="rm -iv".

I’ve taken to heart the adage, which I can’t attribute precisely, that anything typed more than a couple times on the commandline should be shortened to save future keystrokes. To prove this point, I currently have greater than 100 aliases and growing. Note that the following is from dumping all of my aliases to STDOUT via alias > jist -p -c and is not representative of one of my alias files. (For that, see the second inset codeblock).Here’s an example of one of my alias files… appropriately called ‘aliases.conf.zsh’:(Original code is at: https://github.com/zph/zph/blob/master/home/.zsh.d/aliases.conf.zsh) So that’s ‘aliases’. I find that they’re easier to keep organized if I setup a dedicated ‘.zsh.d’ folder. By which I mean that I created a folder called ‘.zsh.d’ in the base of my home directory with the following layout:

This folder contains all of the ‘zsh’ configuration files on my system other than ‘.zshrc’. Each general category of config files gets its own file, ie git.zsh, ruby.zsh, homebrew.zsh, etc. Helps me keep everything organized =D. I set the following lines in my ‘~/.zshrc’ to source each file within the ‘.zsh.d’ directory that has the extension of ‘.zsh’

_source_zshd(){
# Finally, source all the files in zsh.d (ALPHA order)
  for zshd in $(find ~/.zsh.d/*.zsh | sort );
    do source "${zshd}"
  done
}

This isn’t necessary, but it helps me keep the individual configurations nicely ordered. To recap, use aliases. Use the daylights out of them. Make your shell a finely honed tool that’s nicely crafted to your needs. Oh yeah… do the same with ‘functions’ which I can cover in a future blog post. And look into FASD because it’s awesome like Narwhals! Questions, thoughts, jeers, feedback… catch me on ‘ye ol Twitters’ @_ZPH

14 Feb 2013

Make the Shell Your $HOME

Let’s talk about how to use the shell. It’s a dirty secret that many of the repetitive commands used in the day-to-day world of developers are rudimentary shell commands.

Think of the activity of making a new folder and moving into that folder to start writing code, it looks something like this: mkdir new_project; cd new_project; vim new_project_code_file.rb.

Tighten it up and pay attention to simple improvements in the shell environment. ( My experiences are with BASH and ZSH ). That command becomes the following command with a little bit of improved shell configs: mkdircd new_project (makes dir and cds in the same command); e !!1_code_file.rb.

What’s in this shorter series of commands? Let’s break it down to parts:

  • mkdir and cd are replaced by a single new command mkdircd. We save the repetition of typing the folder name twice by compressing the command into a single request. It’s accomplished by setting up an alias in the shell configs.
  • vim new_project_code_file.rb is replaced with e !!1_code_file.rb. The e command is another shell tweak of mine. e is a shell function that steps in for the vim command and opens files for editing in a more intelligent way. See the source here: e (Note that it also depends upon the wonderful FASD utility available on Github.com).
  • !!1 becomes the first argument from the prior line, i.e. the first item after the command itself. Using !!1, !!2, !!3 has saved me countless seconds of retyping the same long folder/file names.

So, make the shell work for you! If you’re spending time in a new envionrment like the Linux Terminal or OSX’s Terminal, learn to configure that BASH or ZSH environment.

It will pay off nicely!

If you liked the article, follow me on Twitter at @_ZPH.

29 Dec 2012

Trello-Archiver from Scratch

Trello-Archiver is a set of Ruby scripts that allows one to export their Trello board into a CSV, TSV, or XLSX file. It was started by another person on Github.com named Mad_Typist. Early on in the process I sent them a pull request to contribute my changes back… but they don’t seem to be around. Given that, I’ve forked the project and continued to develop it for my own benefit.

Trello-Archiver relies on a few conventions that exist in the Ruby world. 1. Project dependencies are managed with Bundler 2. A semi-recent version of Ruby is used >= 1.9.2

To use the project on a system that isn’t setup for Ruby development, the first step is to install a recent version of Ruby. I prefer to use a tool called Ruby Version Manager for this RVM.

Install a sane Ruby Version Manager along with Recent Rubycurl -L https://get.rvm.io | bash -s stable --ruby

Source the installed script to allow for using RVM this first time w/ this command: source ~/.rvm/scripts/rvm

Future logins will automatically source RVM using your bashrc or zshrc.

The next step is to clone the Trello-Archiver repo from Github.

Then cd trello-archiver and run bundle install. Bundler installs the necessary dependencies for Trello-Archiver. The nice feature about Bundler is that it manages individual projects’ dependencies independently :).

Next, cp config.example.yml config.yml. Config.example.yml is the template for where the authentication credentials are stored along with any default settings.

Open up config.yml with your favorite text editor. Instructions are inclosed in there for how to gather your secret credentials from Trello.com.

First, open https://trello.com/1/appKey/generate in your web browser. Copy the first key as your ‘public key’ and the second (longer) key as your ‘private_key’.

Next we need to enter those values into a special url: https://trello.com/1/connect?key=PUBLIC_KEY_FROM_ABOVE&name=ANYNAME&response_type=token&scope=read,account&expiration=never The two values that need replacing in this URL are PUBLIC_KEY_FROM_ABOVE nd ANYNAME. ANYNAME is a placeholder for any text that you want to use to identify this key, ie it could be TrelloArchiver. Also note that this is authorizing Read-Only access. This way, even if there’s a bug in the software the token won’t allow changes to your Trello information.

Past that value into your web browser and click allow on the confirmation webpage.

The following page that loads has your ‘access_token_key’. Copy this value into your config.yml. Save config.yml and exit to commandline.

That’s it for authentication setup! Make sure to be careful with this config.yml and don’t go adding it to any public git repos ;).

Now for backing things up :). Decide if you want a specific board backed up or all of them.

Let’s backup a single board: -ruby bin/trello_backup.rb -Choose your board by number and hit enter -Enter a filename or not (it defaults to name of board and date stamp) -Defaults to exporting as XLSX (this is easily changed in script).

Now let’s backup all the boards: -ruby bin/trello-autoarchive.rb -Sit back and wait for it to finish! -It’s set to backup all boards with default names and using XLSX filetype.

Caveats: It saves these files into the current folder. Apparently I broke CSV functionality with some recent changes. I’ll get it working again. The spreadsheet structure is what works for me. If you need a different configuration, please let me know and I’ll look into it. Not all info is backed up. For full backup, download your JSON export from Trello. The main thing that I didn’t include in Trello-Archiver is a set of timestamps for when a Card is moved to each List. This could be a future feature if others have a need for it.

Below is a Gist showing the shell commands

25 Dec 2012

RCodeTools - How to get them working in VIM

RCodeTools is an add-on for popular neckbeard text editors such as VIM and Emacs. (My preference currently being VIM). I came across it through RubyTapas Screencasts by Avdi Grimm. He uses it extensively in his screencasts for crystal clear explanations and live displays of code execution.

RCodeTools adds hash comments (ie #=\>) to the text editor buffer. Then when another command is executed, the whole buffer is evaluated and the area beyond the hash comment is populated with the output from that specific line(s) of code.

It’s remarkably helpful for explaining and demonstrating code because it avoids any context shift. The comments serve to demonstrate the code’s state at any given moment!

Avdi Grimm uses RCodeTools inside Emacs in the screencasts and I was eager to begin using RCodeTools myself. I had difficulty getting the plugin to work in VIM, resulting in attempts to reinstall VIM, modify the $PATH, etc.

Turns out there is a simpler way: 1. Install RCodeTools into your ~/.vim/ folder. Specifically this means the vimscript files. 2. Install vim-rvm for managing path. 3. Gem install rcodetools inside your standard RVM ruby. 4. Add mappings to VIMRC. Included are my mappings (which are recommended by the README.vim)

I look forward to putting this addon to good use at future CodeRetreats! And it’s the perfect compliment to screencasting.

Thanks Avdi for drawing my attention to this wonderful tool.

24 Dec 2012

Ruby Off The Rails - Ep. 1 -- Zero to Ruby -- Pt. 2

Here’s the second installment, briefly covering installing RVM and an approachable text editor (Sublime Text 2). I advocate for focusing learning on one area at a time which means learn Ruby rather than starting to learn Ruby and VIM at the same time. (Learn VIM or EMACS once you’re further along with the language itself and see it as a long term investment in your productivity and happiness as a code crafter). Ep. 2 – Zero to Ruby – Pt. 2 Ruby Off the Rails - Ep 2. - Zero to Ruby Pt. 2 from ZPH on Vimeo. Here’s the source for the commands used in the episode:

23 Dec 2012

Ruby Off The Rails - Ep. 1 -- Zero to Ruby -- Pt. 1

Ruby Off The Rails is a new series of screencasts that I will be publishing. The first cluster of videos will address the problem of getting started with Ruby development by using a virtual installation of Ubuntu Linux running inside Virtualbox. It’s simpler than it sounds and it preserves the integrity of your everyday system. Here’s the first installment, enjoy! (I recommend opening the video for fullscreen display to better view the text)

Ruby Off the Rails - Ep. 1 - Zero to Ruby - Part 1 from ZPH on Vimeo.

Commands from Ep. 1

22 Dec 2012

JDBC, MsSQL, and JRuby, Oh My!

I was recently in a position of needing to access information from a legacy database on a Microsoft SQL Server ( 2005 ). Since I’d prefer that the contact came from a *NIX system (probably Ubuntu), I checked out ODBC and JDBC as options. Based on a couple of blog entries by fellow Rubyists and ODBC users, the ODBC configuration files add an extra layer of complexity for the database connection. I chose to do it with JDBC because it allows those settings to be configured in the script/config itself. Also, with the sqljdbc*.jar running on the JVM we have platform independence. Now, on to the implementation:

  • On the SQL Server box
  • Allow mixed authentication (it appears that JDBC does not interface with Windows User credentials based on cursory research)
  • Setup a unique SQL Server Auth User who has the appropriate degree of authorization on the database. Consider read-only if your application is limited to querying the DB rather than writing to it. This will limit the risk of inadvertent writes.
  • Depending on setup, you may need to open ports 1433 and 1434 on the SQL Server’s firewall. If possible, only do this step on an internally facing instance of the SQL Server. (Less attack surface to the internet is a good idea).
  • Test the connection by using SQL User authentication when connecting with the builtin Microsoft SQL Server Management program. If all goes well, this tells you that it’s configured correctly when accessing from the same system. Next to connect from a remote system. You’ll need the following pieces of information: the database’s name, the database ip address, the database instance name (ie defaults to SQLEXPRESS when using SQL Express), the Sql username and password
  • On the remote system (the Linux box)
  • Download and extract the following file from Microsoft, this is the JDBC adapter file that they provide http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=21599 OR the direct link at the time of this article: http://download.microsoft.com/download/D/6/A/D6A241AC-433E-4CD2-A1CE-50177E8428F0/1033/sqljdbc_3.0.1301.101_enu.tar.gz Extract that into a convenient place in near where your connection code exists. I chose to use a standard Ruby project layout as follows: |-- Gemfile |-- Gemfile.lock |-- Guardfile |-- README.md |-- bin |-- features |-- jars | |-- sqljdbc.jar | |-- sqljdbc4.jar |– sqljdbc_3.0.1301.101_enu.tar.gz |– lib | |– README | |– jruby_demo.rb | |– tds_demo.rb |-- demo.rb |-- spec – spec_helper.rb 7 directories, 16 files Notice that the sqljdbc\*.jar files are inside jars which is on the same level of nesting as the lib directory. This is important when referencing their location in the code. Then install JRuby if not currently installed. I use [RVM](www.rvm.io) for easy Ruby switching. (Thanks to Keith Burnett for posting a comment indicating that this portion was out of order... silly me!) Once the JARs are extracted, runbundle install` to request that Bundler install the required dependencies. Next modify the following script to help you get started. Note that you’ll need to replace the values of the CONSTANTS as appropriate for your IP addresses, username, passwords, etc.Of further note is that a class will need to be defined for each table in the database that you wish to access. I briefly outline this in the comments of the code. This is fairly standard ActiveRecord use. Questions, comments, post them here or to me on Twitter @_ZPH.