Screw diamonds. Ruby is a girl's best friend.

Surfing the waters of programming

Sorting Hash Values

| Comments

Have a huge Array of hashed and need to find how many times the same event occur?

1
2
3
h = [ {"a" => 10}, {"b" => 20}, {"a" => 10}, {"b" => 20}, {"c" => 30}, {"a" => 10} ]
count_h = h.inject(Hash.new(0)){ |hash, i| hash[i] += 1; hash }
puts h                 #=> {{"a"=>10}=>3, {"b"=>20}=>2, {"c"=>30}=>1} 

Looking for awesome ways to sort Hash by its values?

1
2
3
h = {{"a"=>10}=>3, {"c"=>30}=>1, {"b"=>20}=>2}
Hash[h.sort_by {|key, value| -value }] #=> will sort in descending order
p h #=> {{"a"=>10}=>3, {"b"=>20}=>2, {"c"=>30}=>1} 

Bash and Zsh

| Comments

As I’ve been struggling with setting up my new Mac to be proper RoR utility I need it to be, I can across Laptop script that took care of most installation work for me. It did prompt me to switch to zsh.

For reasons yet unknown to me, zsh doesn’t follow instructions in .bash_profile. And I like my aliases and prompt line settings, so I’m going to switch it back to bash

1
exec bash

When I figure out what is the power of zsh and would like to switch back to it:

1
exec zsh

To set bash back as a default login shell

1
chsh -s /bin/bash

Ruby Exercise 1: Triangle of Numbers

| Comments

Fun exercise that will allow you to practice iteration and loops as well as look into ARGV in Ruby. I will include my detailed logic how I came to resolve this issue because I find it most helpful when learning new concepts in any language to see all nuts and bolts that went into it.

Assignment:
Create an application that will take a number in Terminal and output the same number of lines with the same number of digits in each line. Note: elements continue to grow in order.

Enter in terminal:

1
$ ruby triangle.rb 4

Expected result:

1
2
3
4
1
2 3
4 5 6
7 8 9 10

Organize Your Development Folder

| Comments

While setting up my new Mac, I came to the task of rebuilding my development folder. Most developers will find what works for them. I’m going to provide a list that I am starting with for you to use at your discretion.

Testing Tools in Rails Application Development

| Comments

Did you ever wake up and thought “Today, I’m going to write the entire app in one attempt”? How did it go for you?

One of the hardest thing for me while learning coding was to understand what my lines of code actually do. I would write ten lines of code only to see it do nothing, and have no idea where to start debugging it. I would assume my entire logic was incorrect, erase it all only to find out later that I used .each instead of .map.