Tuesday, 30 August 2011

Ruby: strings

Single-quoted strings only support two escape sequences: two consecutive backslashes are replaced by a single backslash, and a backslash followed by a single quote becomes a single quote.

Double-quoted strings support more escape sequences, like "\n". It also allows string interpolation #{expr}. If the code is just a global variable, a class variable, or an instance variable, you can omit the braces.

%q and %Q start delimited single- and double-quoted strings as well.
%q/general single-quoted string/
%Q!general double-quoted sting!
%{Seconds/day: #{24 * 60 * 60}} # => Q can be ommitted.


You can also construct a string using a here document:
string = <<END_OF_STRING
    The body of the string
    is the input lines up to 
    one starting with the same
    text that followed the '<<'
END_OF_STRING

Normally, the terminator of the here document must start in the first column. However if you put a minus sign after the << characters, you can indent the terminator:
string = <<-END_OF_STRING
    The body of the string
    is the input lines up to 
    one starting with the same
    text that followed the '<<'
    END_OF_STRING

To initialize an array of strings, you can use %w:
%w(foo bar) # => ["foo", "bar"]

Installing rubygems

Installing rubygems on Ubuntu 11.04 takes several steps:

$ sudo apt-get install rubygems rubygems-update
$ gem env | grep EXECUTABLE
// cd to the executable directory
$ update_rubygems 
// now rubygems will be installed in the correct directory.

Saturday, 27 August 2011

Remote Accessing Ubuntu Desktop

Today my Macbook Pro didn't boot up with stripes on the initial gray screen. I took it to the Apple Store and the technician told me that it is a specific failure on the Main Logic Board. However the replacement is covered even thought it is already out of warranty, so I don't need to pay anything for the replacement. :D

But I'll be without my laptop for 5-7 days. :( I have a Chrome notebook with Ubuntu installed on it, but I'm afraid it's going to be laggy if I run Eclipse on it, so I looked into remote accessing my desktop in my lab. I tried the default Remote Desktop Viewer application that came with Ubuntu, but it didn't work: my notebook doesn't refresh the screen after I clicked something on the remote display. I also tried the guide on using x11vnc, but was also out of luck. I used TightVNC's vncviewer. It asked for a password and I wasn't sure what password to use. I tried a few, but all failed.

Finally, I installed the NX free edition and client and it works pretty well. The installation and configuration are very easy too.

Thursday, 18 August 2011

Java ArrayList copy constructor

The copy constructor of ArrayList in Java provides a shallow copy of the elements in the list. This is kind of expected, but I stilled wrote a test to verify this.

public class Test {
  public static void main(String[] args) {
    ArrayList<Point> list1 = new ArrayList<Point>();
    list1.add(new Point(1, 2));
    System.out.println("list1: " + list1);
    ArrayList<Point> list2 = new ArrayList<Point>(list1);
    list2.get(0).x = 3;
    list2.get(0).y = 4;
    System.out.println("list1: " + list1);
    System.out.println("list2: " + list2);
  }
}

Output:
list1: [java.awt.Point[x=1,y=2]]
list1: [java.awt.Point[x=3,y=4]]
list2: [java.awt.Point[x=3,y=4]]

Cloning an array of primitives, like int, is deep cloning. For example:
public class Test {
  public static void main(String args[]) {
    int[] a = {1, 2, 3, 4};
    int[] b = a.clone();
    b[0] = 5;
    System.out.println("a = " + Arrays.toString(a));
    System.out.println("b = " + Arrays.toString(b));
  }
}

The output is:
a = [1, 2, 3, 4]
b = [5, 2, 3, 4]

Friday, 12 August 2011

Notes on Robert Martin interview by the Pragmatic Bookshelf

Podcast: http://pragprog.com/podcasts/show/32

1. Programming is a craft and you do it for the shear love of it. Why would you want to do anything else? Why don't you perfect it. If you love doing it, why changing to another field like management?

2. Learn a new language every year. Clojure is an interesting language to know. Learn functional languages.

3. Corporate languages like Java and C# will become legacies.

4. SOLID: principles of organizing objects in a object oriented language: what should be in a class and what the relationships between objects ought to be.
Single responsibility, Open closed, Liskov substitution principle, Dependency inversion, interface segregation

5. TDD

6. The Structure and Interpretation of Computer Programs - by Abelson and Sussman