Ubuntu Nvidia Driver Problems

linux No Comments »

Sometimes after apply updates to my ubuntu desktop via the package manager the nvidia driver seems to get ‘lost’. This article describes a solution. My variation on that theme is:

sudo aptitude search linux-restricted-modules

have a bit of a look at what is available. For me, this last time I needed:

sudo aptitude install linux-restricted-modules-2.6.22-15-generic

Then

 sudo apt-get install nvidia-glx

?

http://www.nvidia.com/Download/index.aspx?lang=en-us

download driver

stop x server

sudo /etc/init.d/gdm stop (for gnome)

sudo sh

start x server

sudo /etc/init.d/gdm start (for gnome)

Ruby alias method stack level too deep

ruby No Comments »

I’ve just run into a problem where I created an infinite loop by aliasing a method an then redefining the original method.

class String
  alias old_stem stem
  def stem
    # impl not shown
  end
end

If this code is run twice (most probably because the file is loaded twice) then the second time the alias will make old_stem refer to my new definition of stem, creating an infinite loop. Why was the file loaded twice? Because I had a (unnecessary) circle loop which meant that the file in question (’a') required file b; file b required file a via a different path (eg ‘foo/a’). Ruby require will reload the same file twice if they are referred to using different paths, even if the expanded path is the same (see ruby’s require doesn’t expand paths)

My solution in the case is to remove the circle loop as it was cruft that I should have already removed. A more general and explicit solution (courtesy of ruby forum) is:

alias_method(:link_to_original, :link_to) unless method_defined?(:link_to_original)

Ruby mocking via extend

ruby No Comments »

Jay Fields describes some interesting ways of redefining methods in Ruby. The last one was most interesting to me, particularly considering recent explorations into alternate mocking techniques. You can use this technique to redefine a method an then later reinstate the previous behaviour. Don’t know if it will ever be really useful, but I thought it was cool nevertheless.

class PrintHey
  def do
    puts "hey"
  end
end

PrintHey.new.do

unsuprisingly prints “hey”

module PrintDoDeeDo
  def do
    super
    puts "do dee do"
  end
end

extended_ph = PrintHey.new.extend(PrintDoDeeDo)
extended_ph.do

prints “hey”
“do dee do”

module Reinstate_PrintHey
  def do
    @print_hey_do ||= PrintHey.instance_method(:do).bind(self)
    @print_hey_do.call
  end
end

extended_ph.extend(Reinstate_PrintHey).do

prints “hey”. back to the original behaviour.

Class based mocking in Ruby

ruby 1 Comment »

Yesterday I wanted to mock this method:

class HtmlFromUrl
  def get_page
    #download page from web
    ...
  end
end

It gets called here (in HtmlFromUrl.new):

class WebPage
  def html
    @html ||= HtmlFromUrl.new(url)
  end
end

I didn’t want to use a traditional mock object. I want to test that an object of the correct class was created in the ‘html’ method (this method is actually much more complicated than what is shown above) but I didn’t want to actually download a page from the web. This is the way I ended up doing this:

Object.mock(HtmlFromUrl, :get_page, %Q{"<html><head/><body><p>content</p></body></html>"})

and later

Object.unmock

This is the basis of the mock method
class Class
def mock(klass, method_name, new_method_body)
mock_class = Class.new(klass)
mock_class.class_eval(%Q{
def #{method_name.to_s}(*args)
#{new_method_body}
end})
set_constant(klass.name, mock_class)
end
end

The creates a new anonymous class that inherits from the class we wish to mock, defines the method that we want to mock and sets the old class constant name to refer to the new class. Seems to work pretty well, code and tests here

Fixing VIM single repeat pause problem

linux, vim No Comments »

I switched over to ubuntu linux from windows xp about 6 months and soon after noticed a problem with VIM. The single repeat (”.”) command would not complete immediately. Instead it would take about 1 sec or until I pressed another key. A quick search for a solution turned up nothing; I decided that I would just put up with it…. until today.

Today I am going to fix it, my confidence is boosted after discovering this command

vim -N -u NONE -U NONE

which runs vim with out any .(g)vimrc customizations (or plugins) and

vim -V2

which runs in debug log mode. The problem does not occur when running vim -N -u NONE -U NONE but it does still occur when I run normal vim with all the lines in my .vimrc and .gvimrc commented out. So I guess the problem is in some plugin that I’ve installed….

Didn’t get anything particularly useful out Vim -V and tried to do some profiling using :prof start from within vim but this didn’t produce any output, I think that It might be more for plugin developers than plugin users. So I renamed ~/.vim/plugin; problem goes away! Turns out that a plugin that I don’t use, BlockComment uses .c and .C as commands. When I pressed ‘.’ BlockComment was forcing vim to wait for bit to see if a ‘c’ came along next. I think that its a pretty fucked way of doing things but I guess I you can’t really be too critical to someone putting their work out there for the benefit of the rest of us.

When I setup my linux box I went through a phase of just installing a bunch of plugins that sounded useful without really looking into them in too much depth (or even getting in the habit of using them). I’m going to be a bit more conscious of the plugins that I install in the future.

Subclassing Built-in Ruby Classes

ruby No Comments »

Previously I’ve run into problems subclassing ruby strings. This time its arrays. In both cases the root of the problem is that some methods for built in classes create new objects or object duplicates and the class of these objects is not always what you would like them to be. In the case of arrays the ‘[]’ method returns a new object of the same class with the selected elements. Normally the object will be of the class ‘Array’ but the object is a subclass of Array then the new object’s class will be this subclass. However, when the object is created the normal initializer is not called. This means that any instance variables the subclass may define are set to nil. This can lead to unexpected results. The solution is to redefine the ‘[]’ method in any Array subclasses that contain instance variables:

class MyArray < Array
  attr_reader :label
  def initialize(label, array=[])
    super(array)
    @label = label
  end

  def [](first, size=nil)
      return super(first) if size == nil
      self.class.new(label, Array.new(super(first, size)))
  end
end

An alternative is to just return an array:

  def [](first, size=nil)
      return super(first) if size == nil
      Array.new(super(first, size))
  end

Inheritance and Class Variables in Ruby

ruby No Comments »

The “class << self" idiom in ruby creates a special metaclass for the current class. The data held in the instance variables defined within this metaclass is not shared by subclass of the current class. If you want to share data between sub and super classes use a @@ variable.

 class A
    class << self
      def set_value(v)
        @value = v
      end

      def value
        @value
      end
    end
  end

  class B < A
    set_value(5)
  end

  class C < B
  end

  puts B.value
      > ‘5′
  puts C.value
      > ‘nil’

whereas

 class A
    @@value = nil

    class << self
      def set_value(v)
        @@value = v
      end

      def value
        @@value
      end
    end
  end

  class B < A
    set_value(5)
  end

  class C < B
  end

 puts B.value
     > ‘5′
  puts C.value
      > ‘5′

SQLite3 execute! and MisuseException

ruby No Comments »

I’ve been having problems with SQLite3::MisuseException in some ruby code using an sqlite database. The bottom line is that if a prepared statement execute! method throws an exception then reset! must be called before anything other calls are made to the database.

This test case illustrates the problem:

require 'rubygems'
require 'sqlite3'
require 'test/unit'

class SqlLite3Test < Test::Unit::TestCase
  def teardown
    File.delete('sqlite_test.db')
  end

  def test_execute_failure
    db = SQLite3::Database.new('sqlite_test.db')

    db.execute('create table table1 ( id integer primary key autoincrement, data text unique)')

    table1_insert = db.prepare('insert into table1 (data) values (?)')

    table1_insert.execute!('a')
    begin
      table1_insert.execute!('a')
    rescue SQLite3::SQLException
      # table1_insert.reset!       uncommenting this will enable the test to pass
      assert_nothing_raised{ table1_insert.execute!('b') }
    end
  end
end

XARGS

linux, unix No Comments »

I always forget about xargs. It can be very handy when trying to do things efficiently on the unix command line, particularly with pipes. Example, remove all the swap files in a directory tree:


find . -name "*.swp" | xargs rm

Ruby Symbols, IDs and C Extensions

ruby No Comments »

When working with ruby extensions in C don’t every try to pass a symbol to a C function as an ‘ID’. For example:

C Code:

static VALUE id_convert_to_name(VALUE self, ID mid)
{
    return rb_str_new2(rb_id2name(mid));
}

void Init_id_convert()
{
    VALUE cIdConvert = rb_define_class("IdConvert", rb_cObject);
    rb_define_method(cIdConvert, "to_name", id_convert_to_name_id, 1);
}

Ruby code:

id_convert = IdConvert.new
assert('to_s', id_convert.to_name(:to_s))  # problem

If you’re lucky you will get a runtime error when you call the method ‘to_name’ in ruby. Or it might just not work leaving you scratching your head as to why.
Here’s a version that works:

static VALUE id_convert_to_name(VALUE self, VALUE mid)
{
    return rb_str_new2(rb_id2name(NUM2INT(mid)));
}

void Init_id_convert()
{
    VALUE cIdConvert = rb_define_class("IdConvert", rb_cObject);
    rb_define_method(cIdConvert, "to_name", id_convert_to_name_id, 1);
}

To put it another way, no C functions that are going to be exposed as ruby methods should accept ID paramaters, every parameter must be a VALUE.

ruby, symbol, ID, C extension


WordPress Theme & Icons by N.Design Studio
Entries RSS Comments RSS Login