Archive for July, 2010

Ruby post-Python: first impressions

July 16th, 2010 7 comments

I’ve been using Python for a while and really like it. I’d heard a lot of good stuff about Ruby so decided to have a go to see what all the fuss was about. The list of differences between Python and Ruby on was useful in writing this post. Here are my first impressions:

  • One of the things that always bugged me about Python is its lack of proper lambdas. Sure, you can use a locally defined function instead, but it doesn’t give you the kind of fluency you can achieve programming in Scheme or JavaScript, say. I haven’t done much Ruby yet, but it seems there are various ways to do lambdas/closure, in a more flexible way than Python. This wouldn’t have mattered to me, but since I’ve been working through SICP and using JavaScript more, I’ve been thinking in closures.

  • This doesn’t really set it apart from Python, but it does seem to have good consistent coding conventions, which I think are important for a language. And I like the fact that capitalised class names and lowercase variables are enforced by the language.

  • I used to think it sounded dangerous, but I like the fact that parentheses are optional for function calls. I can see how this can make Ruby a very expressive language.

  • I’ve never understood why in Python objects have a __len__() method, but the standard way is to use the len() function to access it. Why not treat objects like objects and use method calls?

  • One type of array, not lists and tuples. Minor headache resolved!

  • Parsed and unparsed strings like in PHP, making use of the distinction between single and double quotes. Useful.

  • Private methods – thank you!

  • 0 as a truthy value – not sure about this. Although I suppose it’s clearer to say if count > 0 rather than if count.

  • elsif instead of elif. That’s going to cause me a lot of typos.

Categories: Programming Tags: , ,

Making Windows more Unixy

July 14th, 2010 3 comments

At work I’m stuck on Windows (XP). At home I use Linux (Arch) and Windows (7). I still use Windows because I need to check that software I write runs on Windows, and certain things like iTunes and Spotify still work much better on Windows. I hate developing on Windows though.

I, like many others, would like to be able to have a Unixy (I didn’t think it was a real word, but look!) experience on Windows, so I (like many others) installed Cygwin.

Installing Cygwin

Installing Cygwin’s pretty easy. Just download setup.exe (using the link “Install or update now! (using setup.exe)”) and run it. Follow the instructions, using the suggested values, until you get to Select Packages. Select Packages is a bit like apt-get on Debian or Ubuntu, or Pacman on Arch Linux. It provides an interface for installing and uninstalling packages for your Cygwin installation. For now, go with the defaults, but add Shells -> chere (see below for why). To install chere, click on the text that says “Skip”. This will change to the latest version number of chere. You can run setup.exe again any time without reinstalling Cygwin, so don’t feel like you need to find everything you’re ever going to need. Now click next, and be prepared to wait a looooong time (it could take longer than an hour).

Open Cygwin Here!

There’s a PowerToy for Windows XP which will allow you to right click on a folder and open a command window. On Windows Vista and 7 this is built in. It’s hidden unless you hold Ctrl+Shift when right clicking. It would be even more useful if you could open a Cygwin shell in this way too. Luckily, you can! That’s what chere is for. Just installing chere in Cygwin won’t cut it though, you need to install it into Windows. To do this, run chere -i (for install) in Cygwin. On Windows Vista or 7 you’ll need to run Cygwin as administrator (right click the icon and select “Run as administrator”) for this to work.

Open Explorer Here!

I’d like to be able to do the opposite – that is, open an explorer.exe window with the working directory of the Cygwin shell. This is fairly easy to do as a bash script. Make a new file called openexp or something equally cryptic and save it in /bin (this is a Cygwin dir). Make it executable (chmod +x /bin/openexp) and put the following line in it:

explorer $(cygpath --windows $(pwd))

Now, when you want to open explorer from Cygwin just run openexp.

If you’re new to Bash you may need this explained. Like most codey things, it’s best explained from the inside out.

pwd: pwd stands for present working directory, and outputs the current path.

$(pwd): $() is used for command substitution. $(pwd) is replaced by `/home“ or whatever

cygpath --windows $(pwd): cygpath –windows converts its path argument from Cygwin (Unix) form to Windows form. Its argument will be the working directory in Unix form.

explorer $(cygpath --windows $(pwd)): explorer.exe is Windows Explorer. It takes a directory as an argument – this will be the Windows formatted version of the working directory.

Categories: Linux Tags: , ,

Samsung Fn hotkeys and Arch Linux

July 14th, 2010 3 comments

I’ve got a new Samsung R530 laptop. The first thing I did when I got it was to install Arch Linux (my Linux distro du jour). I nearly broke everything when I installed Grub in the Windows boot partition (I think Grub and NTLDR were fighting), but it all turned out well in the end.

I really like developing in Linux – it really is the perfect development environment. It’s just a pain that everything doesn’t work out of the box. The thing I’ve had the most trouble with is the keyboard ‘Fn’ hotkeys. By this I mean combinations like Fn + Up (brightness up), Fn + F4 (switch display) etc. I’m going to walk through the steps I took to get these keys working in Arch Linux in the hope that someone in the same situation as me may be saved the hours of faffing I’ve had to do.

First, I tried finding out the keycodes using xev and acpi_listen, as suggested by this Super User question. xev showed the volume hotkeys working (which were doing their job anyway), but neither showed anything for the brightness keys. I asked the Arch Linux forum for help and was directed to the Extra Keyboard Keys Arch Wiki page. There I learnt my first lesson:

A scancode is the lowest identification number for a key. If a key doesn’t have a scancode then we can’t do anything because it means that the kernel doesn’t see it.

A keycode is the second level of identification for a key, a keycode corresponds to a function.

A symbol is the third level of identification for a key, it is the way Xorg refers to keys.

I used showkey to find out if the problem keys had keycodes, and discovered that they didn’t. If a key doesn’t have a keycode, it can still have a scancode. If it doesn’t have a scancode then the kernel just doesn’t see it, and you’re screwed. To find out if a key has a scancode, run dmesg | tail -5 after pressing one of the keys. This should tell you what the scancode is, and tell you how to map the scancode to a keycode.

So, at this stage I was able to discover the scancodes of all the non-functioning hotkeys. I made a list a bit like this:

  • Fn + F2: Show battery – e003
  • Fn + F4: Switch display – e002
  • Fn + F5: Backlight on/off – e004
  • etc…

The next step was to follow the instructions in Map scancodes to keycodes. This told me to look in /lib/udev/keymaps. I found samsung-other in there, which contained the following:

0x82 switchvideomode # Fn+F4 CRT/LCD (high keycode: "displaytoggle")
0x83 battery # Fn+F2
0x84 prog1 # Fn+F5 backlight on/off

In fact, every item in samsung-other matched the keycodes I’d found. Great! The next step is to tell udev to use these keycodes. To do this, make a new file in /etc/udev/rules.d/ called 10-local.rules (as suggested in Writing udev rules), and write:

SUBSYSTEM=="input", ATTRS{name}=="AT Translated Set 2 keyboard", RUN+="keymap $name samsung-other"

Replace samsung-other with your keymap if you’re using a different one. Reboot, then run showkey and see if the keys have keycodes (they should do now). If not, you’ll need to look to Map scancodes to keycodes for advice.

At this point, I’ve got working keycodes, but it seems like X is receiving constant key presses when I press one of them, and it’s causing the computer to crash. I don’t actually have a solution to this at the moment. I thought I’d post this in case my working helps anyone else with a similar problem. Maybe it’ll work for you!

Categories: Linux Tags: