Monthly Archives: December 2014

Ruby Array to Hash Using each_with_object

At work I need to do things with arrays and hashes all the time. Something I’ve needed to do a lot of lately is change an array into a hash.

I always forget the most elegant way to do it, I usually end up having at least 3 lines.

Line 1: create a hash, even if only empty

Line 2: loop through the array and adjust the hash

Line 3: return the hash.

Too many lines! So here I am noting the quicker way of doing it so I never forget again!


your_array.each_with_object({}) { |array_item, hash| hash[array_item] = whatever_you_like }

And here is a bit more information about each_with_object.

1 Reply

Javascript/jQuery Debugging Tips

These are just some debugging tips that I always forget how to do, so I am writing them down here!

Testing your selectors 

In the browser console you can use jQuery to check the selectors you are using come back with the right thing! E.g.

console.log($(‘.class_name’).find(‘a’).html());

(Find what’s in the ‘a’ tag within this class or id)

What’s Coming Back in an Ajax Call?

To view what you have been sent from the server
Go to the dev tools in your browser, click on network tab, click on the relevant ajax link, then choose preview.
What is this thing?
console.log(thing.constructor.name)
Get the selected element
If you select an element in the browser, in the console you can access it with

$0
If you then call

dir($0)

It will return every attribute/property of that element.

 

More Chrome Tips

For lots more tips and tricks, check this out.

Leave a reply