rubycoloredglasses


I'm Jason, a web application developer in East Tennessee.


  1. Changing the Default Text Editor

    Certain command line utilities drop into an external text editor program to accept certain types of input. For instance, when using the command ‘crontab -e’ to edit your cron table, your default text editor program will be opened up with the current cron table configuration. The same also applies to the Git versioning system when using the interactive rebase mode. This helps the program avoid supporting it’s own text editor, and allows the user to specify their preferred text editor.

    To specify the default text editor, simply edit or place the following definition inside of the .bash_profile file in your home directory. This example uses ‘/usr/local/bin/mate -w’ to specify that the Textmate editor be used. You may configure this value to reflect the path for Vim, Nano, or any other text editor you wish to use.


  2. Metaclass

    I ran into an instance of meta programming in Ruby today, in the Exceptional Ruby book I’m reading for work. It seems that the theme this week is “you don’t know Ruby as well as you could”.

    I might be wrong in my understanding here, but this is what I understand thus far:

    Ruby stores methods for an object in it’s class, not the object itself. Objects only really store their attributes/variables in memory. However there exists some unseen entity known as the metaclass which belongs to each object, and it can possibly store methods which belong to that object, but not necessarily to that objects class.


  3. Using Super with Ruby class methods

    One of the awesome things about Ruby is that you can over-ride methods you define, or even over-write methods that are built into Ruby.

    This may not be unique with Ruby, but you can also over-ride super class methods in your defined subclass and use ‘super’ to execute the logic defined in the super class version of that method.


  4. Ruby Coloured Glasses

    I’m sorry Taryn. My friend just pointed your site out to me. I swear it was a coincidence. I like your background by the way.


  5. Duplicate associated records when using FactoryGirl

    When I decided to start using tests as part of my development practice, I had the choice of using the default fixture system, or using one of the recommended fixture alternatives, also known as factories. I decided upon using FactoryGirl given it’s popular mention on the web, and one of the projects at work was using it.

    I had read much about the horrors if using fixtures, with the most memorable opinion of them being that they were ‘brittle’. This, and many other complaints, caused me to skip using fixtures and jump directly to using FactoryGirl.


  6. Finding Records without Specific Child in Many-to-Many Relationship

    Okay. Here is a tricky challenge. Let’s say you are coding a blog system where Posts may have many Tags, and a tag can have many posts. Your database would have a ‘posts’ table, a ‘tags’ table, and a ‘post_tags’. With Ruby on Rails this would be configured for an ActiveRecord model using the has_many through method.

    has_many :tags, :through => :post_tags
    

    I only want posts which have an absence of a relationship with a specific record, which is the tag record representing ‘horrible’. How do I query for a list of posts which are absolutely without a specific tag? Like say I have a ‘horrible’ tag, and I want all posts which are not tagged with ‘horrible’. How would I accomplish this?


  7. Listing Gems from Rails Console

    Got this from Stack Overflow, figured it could come in handy at some point in the future.

    Gem.loaded_specs.values.map {|x| "#{x.name} #{x.version}"}
    

  8. Add a Serialized Hash Attribute to a Factory_Girl Definition

    I recently declared an ActiveRecord model which stores a serialized Hash inside of a text field. When I tried to setup a factory for this model using FactoryGirl, I received many syntax errors. This is because FactoryGirl attributes expect a single value or a certain form of code block.

    factory :post do
        title        "Example Post"
        body         "This is the body of the example post"
        meta         { "version" => 2 }
        created_at   "2012-06-01 17:53:13"
      end
    

    To include a hash as an attribute of a factory, declare the Hash separately and then simply assign it directly in the factory definition.


  9. List Sorted Methods in Ruby

    I often use ‘methods’ to get a list of methods available for an object in Ruby, but it can be a pain trying to look through the list for what I want. I wish it outputed in a sorted list straight down the page. This template will help you achieve that. Maybe I should override the ‘methods’ method. Hm…

    "object".methods.sort.each do |method| puts method end
    

    If you want to get only methods with a certain string inside them, use this:

    "object".methods.sort.each do |method| puts method if method.to_s.index('search_string') end
    

  10. Updating a Serialized Object from a Web form

    You may run into a situation where you create some sort of standard Ruby class that you want to associate with an ActiveRecord model. The serialize method allows you to store an object inside of a text field for an ActiveRecord object.

    With Rails 2.3 support for models nested within forms was added, but it’s clear that this support isn’t compatible with serialized objects. In my example below I have a Post model, which represents a simple blog post. The serialized object is instantiated from a custom class called ‘Metadata’, which stores metadata for the post like it’s type and version.