November 2009 Archives

Double Colon

I’m reading Refactoring: Ruby Edition and I came across the double colon (::) operator. I pretty much understand what it means, but I thought I’d do a little more research to get a better handle on its intricacies. I came across the code below at http://en.wikibooks.org/wiki/Ruby_by_examples#:: and I thought I’d try it out in irb. Here is the output:

CONST = ’ out there’

class Inside_one
CONST = proc {’ in there’}
def where_is_my_CONST
::CONST + ’ inside one’
end
end

class Inside_two
CONST = ’ inside two’
def where_is_my_CONST
CONST
end
end

>> puts Inside_one.new.where_is_my_CONST
out there inside one

>> puts Inside_two.new.where_is_my_CONST
inside two

>> puts Object::CONST + Inside_two::CONST
out there inside two

>> puts Inside_two::CONST + CONST
inside two out there

>> puts Inside_one::CONST
#<Proc:0×00000001012ac4a0@(irb):3>

>> puts Inside_one::CONST.call + Inside_two::CONST
in there inside two

Continue Reading…

Posted by Liah on Nov 06, 2009

Search and Product Listing in Zen Cart

I've been working out some bugs in the search functionality that arose from some customization of the product listing. The files I've been editing are scattered across many different folders and I thought it would be helpful to list all the files and what is in them.

The heart of the product listing for both searches and category displays is held in a variable called $listing_sql. There are two files where $listing_sql is built:

includes/index_filters/default_filter.php

includes/modules/pages/advanced_search_result/header_php.php

These files contain conditional statements that determine what SQL query should be executed.

Both files send $listing_sql to:

includes/modules/product_listing.php

This file processes the SQL output into strings of html. Continue Reading…

Posted by Liah on Nov 04, 2009

:symbol to proc

I was tri-programing with S & B today, writing Selenium tests. S typed:

People.all.map(&:name)

into irb to demonstrate a database call. I’ve seen this syntax before, but never understood what it meant. He said he didn’t know, but that it worked. The output is an array of the names of all the People:

["Sandra", "Bob", "Fred", "Rosey"]

B explained that the ampersand followed by a symbol is a concise syntax for mapping the symbol object to a block.

Here is a bit of playing with irb that illustrates how Symbol#to_proc works:

>> People.all => [#<People id: 2, name: "Sandra">, #<People id: 3, name: "Bob">, #<People id: 4, name: "Fred">, #<People id: 5, name: "Rosey">]

>> People.all.map(&:name) => ["Sandra", "Bob", "Fred", "Rosey"]

>> People.all.collect { | t | t.name } => ["Sandra", "Bob", "Fred", "Rosey"]

>> :name.to_proc => #<Proc:0x00002b720b0f45f8@/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/symbol.rb:11>

>> mything = proc { | t | t.name } => #<Proc:0x00002b720f91e950@/home/heroku_rack/lib/console.rb:133>

>> People.all.map(&mything) => ["Sandra", "Bob", "Fred", "Rosey"]

>> People.create!(:name => "Ben") => #<People id: 9, created_at: "2009-11-02 13:25:55", updated_at: "2009-11-02 13:25:55", name: "Ben", creator_id: nil>

>> People.all.map(&mything) => ["Sandra", "Bob", "Fred", "Rosey", "Ben"]

In a nutshell, People.all.map(&:name) gives the same result as People.all.map( | t | t.name)

Continue Reading…

Posted by Liah on Nov 02, 2009