Thursday, January 16, 2014

Ruby, turn array of hashes into single hash

I have the following Array of Hashes:

a = [{:a => 1, :b => "x"}, {:a => 2, :b => "y"}]

I need to turn it into:

z={"x" => 1, "y" => 2} 

or:

z={1 => "x", 2 => "y"}

Can I do this in a clean & functional way?

Something like this:

Hash[a.map(&:values)] # => {1=>"x", 2=>"y"}

if you want the other way:

Hash[a.map(&:values).map(&:reverse)] # => {"x"=>1, "y"=>2}

incorporating the suggestion from @squiguy:

Hash[a.map(&:values)].invert

Wednesday, January 15, 2014

Remap arrow keys to move directory in NERDTree

When using NERDTree, I would like to remap the left & right arrow to move up & down a directory.

I believe the left arrow has to be mapped to ‘u’ yet I do not know how to do this only when NERDTree is the active window.

Thanks!

Try this:

autocmd FileType nerdtree nmap <buffer> <left> u

Sunday, January 5, 2014

php simplexml get a specific item based on the value of a field

Is there a way i can obtain a specific item with SimpleXML ?

For example, i would like to obtain the title of an item having ID set to 12437 with this example xml :

<items>  <item>    <title>blah blah 43534</title>    <id>43534</id>  </item>  <item>    <title>blah blah 12437</title>    <id>12437</id>  </item>  <item>    <title>blah blah 7868</title>    <id>7868</id>  </item></items>

Here are 2 simple ways of doing what you want, one is iterating with each item like this:

<?php$str = <<<XML<items><item><title>blah blah 43534</title><id>43534</id></item><item><title>blah blah 12437</title><id>12437</id></item><item><title>blah blah 7868</title><id>7868</id></item></items>XML;$data = new SimpleXMLElement($str);foreach ($data->item as $item){    if ($item->id == 12437)    {        echo "ID: " . $item->id . "\n";        echo "Title: " . $item->title . "\n";    }}

Live DEMO.

The other would be using an XPath, to pin point the exact data you want like this:

<?php$str = <<<XML<items><item><title>blah blah 43534</title><id>43534</id></item><item><title>blah blah 12437</title><id>12437</id></item><item><title>blah blah 7868</title><id>7868</id></item></items>XML;$data = new SimpleXMLElement($str);// Here we find the element id = 12437 & obtain it's parent$nodes = $data->xpath('//items/item/id[.="12437"]/parent::*');$result = $nodes[0];echo "ID: " . $result->id . "\n";echo "Title: " . $result->title . "\n";

Live DEMO.

Friday, January 3, 2014

Detecting The iPhone Device Type

I’m having an issue when I deploy my app my phone. I am currently building the app on a 3gs, when the view is displayed, it is the iPhone 5 layout that shows, which makes some objects display partially off of the screen.

You can use the [UIDevice currentDevice] class method. This will donate you information approximately the device like systemName, systemVersion, model, etc.

If you need to obtain the screen dimensions instead, you could use the [UIScreen mainScreen] class method. With it you could obtain the applicationFrame or the screen bounds.

Hope this helps!

Unable to reference local variable with JsonBuilder

I have the following code

    AddTicketCommand addTicketCommand = new AddTicketCommand(request.JSON)    JsonBuilder jsonBuilder = new JsonBuilder()    jsonBuilder {        ticket {            subject addTicketCommand.subject            requester {                name currentUser?.name                email currentUser?.emailAddress            }            comment {                body addTicketCommand.comment            }            custom_fields {                [                        {                            id 21857727                            value addTicketCommand.zenRequestType                        },                        {                            id 21854146                            value addTicketCommand.zenProductId                        }                ]            }        }    }

The addTicketCommand object is not null on line 2 yet is undefined within the JsonBuilder closure. Is it not possible to access local variables in groovy from within closure?

You should be able to access addTicketCommand inside the closure as below. Mark the use of “parenthesis” instead of “curly” braces.

AddTicketCommand addTicketCommand = new AddTicketCommand(request.JSON)JsonBuilder jsonBuilder = new JsonBuilder()    jsonBuilder {        ticket {            subject addTicketCommand.subject            requester {                name currentUser?.name                email currentUser?.emailAddress            }            comment {                body addTicketCommand.comment            }            custom_fields ([ //Note the use of parenthesis                {                    id 21857727                    value addTicketCommand.zenRequestType                },                {                    id 21854146                    value addTicketCommand.zenProductId                }            ]) ////Note the use of parenthesis        }    }