Monday 27 February 2012

Ruby: yield statement

A method can invoke an associated block one or more times using the Ruby yield statement. You can think of yield as being something like a method call that invokes the block associated with the call to the method containing the yield. Whenever yield is executed, it invokes the code in the block. When the block exits, control picks back up immediately after the yield. Here's a trivial example:
def three_times
  yield
  yield
  yield
end
three_times { puts "Hello" }
produces:
Hello
Hello
Hello

Thursday 23 February 2012

Difference between ~> and => in Gemfile

Gemfile
~> is used for pessimistic version constraint. Bundler will increment the last digit in the version provided and use that until it reaches a maximum version. So ~> 0.8.5 is semantically equivalent to:
gem "cucumber", ">= 0.8.5", "< 0.9.0"

This means that you are okay with the last digit incrementing to some arbitrary value, but the ones preceding it in the string cannot be greater than what you provided. You might do this if you think that 0.9 version is going to implement some breaking changes, but you know the entire 0.8.x release series is just bugfixes.

Bundler commands
bundle install installs the dependencies specified in the Gemfile if this is the first time you run it (and a Gemfile.lock does not exist). If a Gemfile.lock does exist, and you have not updated you Gemfile, bundler will fetch all remote sources, but use the dependencies specified in the Gemfile.lock instead of resolving dependencies.

References:
http://stackoverflow.com/questions/4292905/what-is-the-difference-between-and-when-specifying-rubygem-in-gemfile

Saturday 18 February 2012

Ubuntu 11.10 boot freezing at Checking Battery State

My Ubuntu on my laptop was not functioning correctly for a week. After login, the resolution is wrong using Unity and I couldn't change the resolution because all the interaction (mouse clicking) was not working. When I switch to GNOME, the resolution is better (usable) but still not correct. So I changed the resolution using the Nvidia setting tool and saved the configuration file (this is a mistake).

After restarting, it froze during boot. I tried a lot of thing, rebooting, going to recovery mode etc. All didn't work. If I end 'e' at the GRUB menu and the enter Ctl+x, it froze at the line "Checking battery state..." After some searching online, I found a solution.

First enter TTY1 by holding down Ctl+Alt+1 when you see it freezes at "Checking battery state...". Then in the command line, redo the nvidia configuration:
$sudo nvidia-xconfig
The output said there were errors in the xconfig settings and a new file is generated. So the problem is really with the Nvidia settings.

Saturday 4 February 2012

Ruby: calling a method

Collecting hash arguments
Ruby doesn't have keyword arguments. But you can achieve the same effect by using hashes. You can place key => value pairs in an argument list, as long as they follow any normal arguments and precede any splat and block arguments. All these pairs will be collected into a single hash and passed as one argument to the method. No braces are needed. There is also the new hash literal syntax in Ruby 1.9: 
class SongList
  def search(field, params)
    # ...
  end
end 

list.search(:title, genre: 'jazz', duration_less_than: 270)

Friday 3 February 2012

How to speak by Patrick Winston

Today I attended Professor Patrick Winston's popular lecture on How to Speak. The lecture theater was packed and people had to sit on the floor including me, but it is really an awesome lecture. Here's the notes on the lecture.

Success = f(Knowledge, Practice, Talent)

  • How to start
    • Don't start with a joke because people may not have started paying attention yet and it will probably fall flat.
    • Start with a menu (agenda) of what you are going to cover.
    • Even better, start with a promise (what the audience will get by the end of the talk).
  • Big 4 (heuristics of giving a good talk)
    • Cycle: repeat the important point several times, and with more details every time.
    • Fence: fence your idea by showing how it is different from others.
    • Punctuation: use verbal punctuation.
    • Question
  • Tools
    • Time & place
      • 11am is a good time to give a talk. 
      • Become familiar with the place and choose a room that can be comfortably full if you can.
    • Board: 
      • using the board can help you control the speed, and it automatically slows you down.
      • Use the board to draw.
      • Use it as a target to point at.
    • Projections
      • Do not read the slides and use fewer words.
      • Be in the image
      • Use simple image
      • Use > 30pt
      • Laser point is a bad idea
    • Props
  • Special cases
    • Oral exams
      • Don't apologize if you don't know something. 
      • Confess instead
      • People who fail usually because they fail to situate in time, space and field.
      • Show your hand: situate your research by showing its relevance and importance.
      • Practice with vicious colleagues
    • Job talk
      • Show vision and steps to achieve it in 5 minutes. For example, you can say "here are 4 steps to achieve the vision. Step 1 & 2 have been done, and I'm going  to focus on step 3, and continue with step 4 in the future."
      • How to end
        • Thank you!
        • Acknowledgement
        • Conclusions
        • Contributions
  • The Star
    • Symbol
    • Slogan
    • Surprise
    • Salient: idea that sticks out
    • Story
  • Stopping
    • Joke is ok
    • Thanks
    • Questions
    • Recite contributions
    • Salute