Ammar's Techno Babble

All kinds of technology related things I learn and jot down.

  • Converting a String to a Valid Date Object

    • 9 Mar 2011
    • 0 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost
    Today at work I had to fix a form that allowed users to enter expiration dates in the past.  Well the form was passing the dates to the controller as a simple string, when I tried to do a comparison on the string it obviously didn't work.  So I had to convert the string into a valid format to compare with a Date object.

    I first thought I could create my Date object like this, but I got an error.

    >> Date.new("03/01/2011")
    NoMethodError: undefined method `-' for "03/01/2011":String

    After some Googling I found the strptime method, you can call it on a Date object, with the string version of the date as a parameter and it outputs a nice, clean Date object that you are able to use in your comparison.

    I did the following:

    date_entered = Date.strptime("{#{params[:subscription][:expires_date]}}", "{ %m/%d/%Y }")

    Then used the date_entered variable in my comparison, like this:

    if(date_entered < Date.today)
      # DO SOMETHING HERE
    end

    • Tweet
  • Preserving Parameters in Failed Form Submission in Rails

    • 22 Feb 2011
    • 0 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost
    I recently ran into a bug at work, I was trying to submit a form that had some hidden variables and when the form would fail saving it would call render :action => "new", which sent the user back to the form on the "new" page.  This is fine but since the form had some hidden fields that were being set by parameters being passed on the url, the hidden fields would fail to get set, causing the object to get saved but without some very important attributes.

    I did some searching and found out that if I wanted to preserve the parameters when a form failed, I would have to set them in the form_for method call, with the url key, like this:

    form_for(@product, :url => { :action => :create, :type => params[:type], :name => params[:name] }) do |f|

    After setting the type and name like this, the parameters were successfully retained.

    • Tweet
  • Understanding Ruby Blocks, Procs and Lambdas

    • 20 Feb 2011
    • 0 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost
    One of the most misunderstood parts of Ruby are Blocks, Procs, and Lambdas.  I've used them in the past a few times, but have never had the time to dig deep and truly understand the differences.  Lately I've been working with Rack and it's forcing me to really understand Blocks, Procs, and Lambdas.  I found a great article by Robert Sosinski, http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas.

    Robert really gives a great explanation of this useful feature.

    • Tweet
  • Postgres pg Gem Install Failure

    • 15 Jan 2011
    • 0 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost
    I ran into a few issues while installing the "pg" gem that is required for communication with the Postgres database.  The error that I was seeing was:

    Can‘t find the ‘libpq-fe.h header
    *** extconf.rb failed ***

    I didn't understanf because I had Postgres installed correctly in the /Library/PostgreSQL directory.  After doing some research I figured it out.  The build architectures between Ruby and Postgres were not matching.

    I ran the following to figure out what the architectures were.

    For Ruby:

    file $(which ruby)

    /Users/ammarmayk/.rvm/rubies/ruby-1.8.7-p330/bin/ruby: Mach-O 64-bit executable x86_64

    For my Postgres 8.4:

    file $(which psql)

    ./psql: Mach-O universal binary with 2 architectures
    ./psql (for architecture ppc):    Mach-O executable ppc
    ./psql (for architecture i386):    Mach-O executable i386

    As you can see Postgres was not build for the x86_64 architecture and that is all I had for my Ruby.  So I decided to upgrade the Postgres to the newest version, 9.0.

    I downloaded the DMG installer from the Postgres site and installed it, I made sure to replace Postgres 8.4 with the new 9.0 in my .profile file.

    I added this to the path /Library/PostgreSQL/9.0/bin.

    Then after this I ran the file command again, this time Postgres showed that it had been built for the x86_64 architecture.

    ammar-yousufs-macbook-pro:~ ammarmayk$ file $(which psql)
    /Library/PostgreSQL/9.0/bin/psql: Mach-O universal binary with 3 architectures
    /Library/PostgreSQL/9.0/bin/psql (for architecture ppc7400):    Mach-O executable ppc
    /Library/PostgreSQL/9.0/bin/psql (for architecture i386):    Mach-O executable i386
    /Library/PostgreSQL/9.0/bin/psql (for architecture x86_64):    Mach-O 64-bit executable x86_64

    Now when I did a "gem install pg" it all went through without any problems.

    Hope that helps anyone having Postgres woes.

    • Tweet
  • RVM and Redline Errors on Snow Leopard

    • 15 Jan 2011
    • 0 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost

    Today I was installing ruby-1.8.7-p330 on my MacBook Pro for my RVM instance.  I kept getting these weird errors about readline.c, I had no idea what the readline.c was or what it was used for.

    readline.c: In function ‘username_completion_proc_call’:
    readline.c:730: error: ‘username_completion_function’ undeclared (first use in this function)
    readline.c:730: error: (Each undeclared identifier is reported only once
    readline.c:730: error: for each function it appears in.)
    make[1]: *** [readline.o] Error 1
    make: *** [all] Error 1

    After some Googl'in I learned that Readline is a C library that is used for reading in a line of text from a provided file handle and returns a character pointer.  When Ruby was trying to build it was having a conflict with the version of Readlines that were installed on the system, there were  two, one that came with OS X and one that was installed my MacPort.

    OS X has it installed in /usr/lib and MacPorts has it installed in /usr/local/lib.

    We have to use the version that was installed by MacPorts.

    Specify the version of Readline to use when installing ruby-1.8.7-p33 like this:

    rvm install ruby-1.8.7-p330 --with-readline-dir=/usr/local
    Once I specified the location of readline the installed went through successfully.

     

    • Tweet
  • Ruby's Dir Class is Awesome

    • 6 Jan 2011
    • 0 Responses
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost
    I'm working on a new project at work and I needed to require all the model classes in our project for a script, so I did it individually one by one, then my friend Tony showed me a nicer way to include all the model files in one line.  Thanks Tony!

    He used Ruby's Dir class, which allows you to interact with the file system's directories. 

    Dir.glob("#{LIBPATH}/../app/models/*.rb").each { |file| require file }

    So now with this one liner I have successfully required all the models in my project.

    To find more about the Dir class you can check out the Ruby doc.
    http://ruby-doc.org/core/classes/Dir.html
    • Tweet
  • Speeding up Project Search in Textmate

    • 5 Jan 2011
    • 2 Responses
    •  views
    • ack textmate
    • Edit
    • Delete
    • Tags
    • Autopost
    So I've been using Textmate for about three years now, overall I've been pretty happy with it, except for the fact that you can't do split screen and the searching is very very slow.  Well for my second problem I've found a solution, a friend of mine pointed me to the "Ack in Project" bundle that uses ack to speed up the searching in your project.  Here is a version of "Ack in Project", that allows you to search in particular filetypes, https://github.com/kemayo/ack-tmbundle

    To install it:
    cd ~/Library/Application\ Support/TextMate/Bundles
    git clone git://github.com/kemayo/ack-tmbundle.git Ack.tmbundle
    Then to find something in your project just enter (Command+Shift+a)

    Screen_shot_2011-01-05_at_6
    • Tweet
  • iSight Camera Problems with Flash 10.6.5

    • 25 Dec 2010
    • 1 Response
    •  views
    • Edit
    • Delete
    • Tags
    • Autopost

    So today I spent about 4 hours trying to get my MacBook Pro's iSight camera to work with sites that use webcams via Flash, sites like yFrog.com and Justin.tv.  After updating Flash to the latest version 10.6.5, everything iSight related stopped responding, no matter what browser I tried, Firefox, Safari or Chrome, all had the same problem.  I tried uninstalling and installing other versions of Flash without luck.

    So then I came across this article in MacWorld, http://www.macworld.com/article/156357/2010/12/isight_flash.html, it seems that this is a pretty big problem that has been affecting many Mac users!  The first thing the article said was to run GoogleVoiceAndVideoUninstaller.app in /Library/Application Support/Google, the second I did this it fixed my problem!  So there seems to be a conflict between Google Video and Flash, something that screws up the iSight driver.  Hope this article solves your issue if you are having trouble.

    • Tweet
  • Getting Google Top Stories with Nokogiri

    • 22 Dec 2010
    • 0 Responses
    •  views
    • google google news nokogiri scrape
    • Edit
    • Delete
    • Tags
    • Autopost

    So I wanted to get the Top Stories from Google News, to my surprise I found out that there was no API available for Google News, I had to resort to a brute force method and scrape.  Here is the scraper the I wrote in about 10 minutes.

    require 'rubygems'
    require 'nokogiri'
    require 'open-uri'

    url = "http://news.google.com/"
    doc = Nokogiri::HTML(open(url))

    doc.css(".topic").each do |topstory|
      item = topstory.at_css("a").text
      puts item
    end

    This produces a nice list for me:

    Spider-Man
    Dow Jones Industrial Average
    Network neutrality
    North Korea
    Missile defense
    Human rights
    Los Angeles
    Philadelphia 76ers
    Green Bay Packers
    Lindsay Lohan

    • Tweet
  • Ruby Inspect Method Behavior

    • 22 Dec 2010
    • 0 Responses
    •  views
    • active record inspect rails ruby
    • Edit
    • Delete
    • Tags
    • Autopost

    So today while at work I was trying to debug a bit of code and see the contents of an Ruby object, which is a simple enough task.  I used the 'inspect' method to print a string representation of the object, this worked perfectly and I was able to see all the attributes of the object in the console.  Then later tonight I was working on Shoutreel and used 'inspect' again, but this time in the View, something that I had never really tried before, and to my surprise it failed to print a string representation of the object.  A hash sign was printed out.  Why is this?

    So is this a bug in Rails, does the 'inspect' method in the view not work for model objects?  My guess is that the inspect method in ActiveRecord, which is what the model object inherits from, has a bug.

    I'll investigate this and write back soon.

    This is the code I had in the view:

    <%= reel.inspect %>
    <%= [ 1, 2, 3..4, 'five' ].inspect %>

    The top line printed the hash, while the bottom line printed out the array as a string.

    • Tweet
  • About

    I am a web developer living outside of Washington DC. I love to keep learning new, better ways to program. Currently I am working with Ruby on Rails, Android SDK, and iOS4. I also own a site called Shoutreel.com, we're trying to create a new type of video site that makes it easy for people to share video recordings with family and friends.

    11256 Views
  • Archive

    • 2011 (10)
      • March (1)
      • February (2)
      • January (7)
    • 2010 (3)
      • December (3)

    Get Updates

    Subscribe via RSS
    Twitter