Thursday, May 30, 2013

Updating my bash

Bash is a command language interpreter (i.e. shell) that executes commands from standard input or file. It was written by Brian Fox to replace the Bourne shell. The name is an acronym for the 'Bourne-Again SHell'  a pun on Stephen Bourne, the author of the current Unix shell sh. (Bash manual)

A few days after I started at 8th Light, Eric updated my bash to make it more readable. It has colors and smilie faces to indicate whether or not the last command was successful. Eric's bash profile is on github in case you want to update yours too. Since then, Eric gave two presentations on Bash which gave me a little confidence on changing mine. Here is what mine looks like currently:


There are several things to note here: 
1. When the shell is able to execute my command successfully, it gives me a green happy face ^_^
2. When it fails, it gives me a red "D'OH" face >_<
3. When I list the directory, the files are in grayish and the directories are cyan
4. It also tells me which branch I'm on and whether or not I made updates on the branch. The "+" show I added files to the directory. 

All the files for these changes are in ~/.bash. You can open it and explore. I made the face and color changes changes in the prompt file. 

While I had some confidence left, I decided to fix the thing that was most annoying to me, the case sensitive auto complete. For example, I have the folder Ruby. If I type "ru" then hit tab, it won't do anything. If I type "Ru" then it will show "Ruby". Nick Burwell had a blog post on this that seems super easy and clean. Note that this command add to the file, so you don't have to worry about overriding the file. Awesome eh? 

echo "set completion-ignore-case On" >> ~/.inputrc

Resource:

Monday, May 20, 2013

Ruby: Loops with value and index

How many way can you loop through an array in Ruby? A lot! I don't want to give a an exact count because I don't think I've learned them all. Here's just a list of the one I came across so far.

Situation:

I have two arrays, secret_code and guess.
secret_code = [2, 4, 6, 8]
guess = [8, 4, 6, 2]
I need to count how many values in the guess array that matches the values and the positions in the secret_code. So for the example above, my method should return 2 because 4 and 6 matches the values and position. 2 and 8 only match the values, but not the positions, so they don't count. This is what the test looks like.
describe MastermindRules do  
  it "counts the exact match when there exact match and value match" do
    secret_code = [2, 4, 6, 8]
    guess = [8, 4, 6, 2]
    MastermindRules.new.exact_match(secret_code, guess).should == 2
  end
end
This is the method api:
class MastermindRules
  
  def exact_match(secret_code, guess)
    
  end
  
end
So how many ways can I write the exact match method? Let's find out!

while loop

  def exact_match(secret_code, guess)
    exact_match = 0
    index = 0

    while index < secret_code.size
        exact_match += 1 if secret_code[index] == guess[index]
        index += 1
    end

    exact_match
  end

until loop

  def exact_match(secret_code, guess)
    exact_match = 0
    index = 0

    until index >= secret_code.size
        exact_match += 1 if secret_code[index] == guess[index]
        index += 1
    end
    
    exact_match
  end
Note that the condition statement is different than the while loop. In the while loop, it says, execute the loop while index is less than secret_code.size. But for the until statement, it says, execute the loop until the index is greater or equal the the code size. Which one do you choose? I prefer the one that is easier to understand. For example, if I have a method call game.over? I would use until game.over? because while sounds a little clunky, while !game.over?. On the other hand, if I have a method call game.in_progress? I would use while game.in_progress? but not until !game.in_progress?. In this case, if I have to choose between while and until, I would choose while.

loop loop

  def exact_match(secret_code, guess)
    exact_match = 0
    index = 0
    
    loop do
     break if index >= secret_code.size 
        exact_match += 1 if secret_code[index] == guess[index]
        index += 1
    end
    
    exact_match
  end
All of these feel a little clunky don't they? I have to initialize the index and manually increment it. The good thing I learned about Ruby is that if it feels clunky, there's probably a better way to write it.

for loop

  def exact_match(secret_code, guess)
    exact_match = 0

    for index in 0...secret_code.size
      exact_match += 1 if secret_code[index] == guess[index]
    end

    exact_match
  end
Note there are three periods for 0...secret_code.size. This gives me a range from 0 to secret_code.size, not including the code size which for me means 0-3. If there are two periods, then the range is 0-4. The two periods will make my test fail because secret_code[4] = nil and guess[4] = nil, which means my exact_match count will be 3. But I don't want that. So be careful with ranges. At the same time, I can also specify an array to loop through. However, this is not flexible for our case because we can't change the secret_code size on the fly.
  def exact_match(secret_code, guess)
    exact_match = 0

    for i in [0, 1, 2, 3]
      exact_match += 1 if secret_code[i] == guess[i]
    end

    exact_match
  end

each_with_index

  def exact_match(secret_code, guess)
    exact_match = 0

    secret_code.each_with_index do |value, index|
      exact_match += 1 if secret_code[index] == guess[index]
    end

    exact_match
  end

range with each

  def exact_match(secret_code, guess)
    exact_match = 0
    
    (0...secret_code.size).each do |index|
      exact_match += 1 if secret_code[index] == guess[index]
    end
    
    exact_match
  end

times

  def exact_match(secret_code, guess)
    exact_match = 0
    
    (secret_code.size).times do |index|
      exact_match += 1 if secret_code[index] == guess[index]
    end
    
    exact_match
  end

upto

  def exact_match(secret_code, guess)
    exact_match = 0
    
    0.upto(secret_code.size-1) do |index|
      exact_match += 1 if secret_code[index] == guess[index]
    end
    
    exact_match
  end

step

  def exact_match(secret_code, guess)
    exact_match = 0
    
    0.step(secret_code.size, 2) do |index|
      exact_match += 1 if secret_code[index] == guess[index]
    end
    
    exact_match
  end
As the name suggest, step is probably better when you want to skip elements in an array, but not when you need to loop through each element.
So with all these looping options, which one do you choose? It's really up to you. I prefer the one that best expresses my intent. For my problem, I chose each_with_index because when I read this, I know that the value and the index is important.
For more details, Alan has a great article on loops in ruby.

Monday, May 13, 2013

Elixir - Coin changer kata

At the end of week two, Eric told me to write the coin changer kata in Elixir to get myself familiar with the language.

Problem
Given an amount of money, write a program to return the least coin possible. For example:
Input: 47 (cents)
Output: 25, 10, 10, 1, 1 (which means 1 quarter, 2 dimes, and 2 pennies)

I did the coin changing kata with Kelly before in Ruby. It wasn't that hard. But Elixir has a different school of thoughts. I have to write the program without using if or regular looping. Instead, I have to use  guard statement and recursion for looping. Here's a video on what I did. 

I can't attached the files at this time. So here's the link:
http://www.youtube.com/watch?v=miN8XTE0XY0
Resource:

Thursday, May 9, 2013

Running PHP with Apache on Mac


  1. Open the terminal
  2. Type "cd /etc/apache2"
  3. Change permission on httpd.conf by typing "sudo chmod 755 httpd.conf"
  4. Open httpd.conf
    1. Search for "#LoadModule php5_module libexec/apache2/libphp5.so" (line 117)
    2. Uncomment the line by removing "#"
  5. To start the server type "sudo apachectl start"
  6. To stop your server type "sudo apachectl stop"
  7. Your files should be saved in "/Library/WebServer/Documents/ "
  8. On your browser, type "localhost"
    1. You should see "It works!"
Testing your server
  1. Create a file call "test.php"
  2. Copy/Type this in
  3. 
    
  4. Save it in "/Library/WebServer/Documents/ 
  5. Open your browser and type "localhost/test.php"
  6. You should see "Please work"
Ryan has a post explaining what each steps mean.

Source:
  • http://www.editrocket.com/articles/php_mac_os_x.html
  • http://www.cyberciti.biz/faq/restarting-apache-under-oxs-commandline/