This are my notes in the fields of computer science and technology. Everything is written with ABSOLUTE NO WARRANTY of fitness for any purpose. Of course, feel free to comment anything.

Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Thursday, July 16, 2009

Tiny implementation of a suffix array in Ruby

class SuffixArray

attr_reader :suf, :string

def initialize(string)
@string = string
@suf = (0..string.size-1).sort_by{|i|@string[i..-1]}
end

end

Saturday, April 11, 2009

Ruby and GUIs

Is there a good ruby GUI toolkit?

I want to write a simple app, with a simple DB backend, just to keep some notes. As I want it to run on my laptop, I don't want any webserver running, so no rails app (and also because I want to do some gui programming, it's years I am only working on console or web apps). 

I started earlier this morning my Google quest to answer this question, I am not so far yet. I am no expert in GUIs. 

Some disorganized thoughts:

- Java of course is a good choice, isn't it? Yes I want to learn Java again (I learned it 10 years ago, and not using it a long time, so I guess my knowledge is totally out-of-date). But now I want just to write a little small app in that simple little lovely ruby language. 

- I guess most Win apps are based on "native" widgets. For that maybe you need VS or the like and I am in this moment in no mood to be a MS fan. And I want something cross-platform. 

- There a libraries, and like always in IT a lot of names and acronymes, just to confuse stupid newbies like me. QT is one, I think, then there are others (I think some GUI library-flame is also the reason why Gnome and KDE are 2 different desktops, isn't it?). So I guess you use one of that with some good ruby bindings and you are on it. Right?

- I read of Shoes: very simple, maybe good idea, but probably just for beginners, I don't know if I want to waste my time with it. Looks like is just something for kids learning programming? 

Monday, August 18, 2008

Default values in Ruby blocks

The ruby parser does not allow default values to be set in blocks, unlike in method signatures. This is of course valid ruby:
def method(a, b=0)
#...
end
but this isn't:
Proc.new {|a, b=0| } ### syntax error!
See e.g. this discussion in Ruby forum.

However it is possible to simulate the behaviour, creating a de facto signature with default values. For example lets say I want a proc accepting the same parameters as a method defined as def a(b, c=1):
lambda do |*args| # simulated signature: |b, c=1|
b, c = args[0], args[1] || 1
end
This has the disadvantage that is not validating the number of arguments, so let's add some validation code to the block:
lambda do |*args| # simulated signature: |b, c=1|
b, c = args[0], args[1] || 1
# validate number of arguments:
err = "wrong number of arguments"
if args.size > 2
raise ArgumentError, "#{err} (#{args.size} for 2)"
elsif args.size == 0
raise ArgumentError, "#{err} (0 for 1)"
end
end
Now the block behaves like if it had the signature |b, c=1|.

Thursday, August 14, 2008

Execute a Rake task in another

It is easy to make one task dependent on another, for example:

task :one => [:two, :three]

executes task two, three, then one (I have to test that the order is really this). But how to execute task ":two" in the middle of the code of another task?

Here is the solution:

desc "This task executes task two in its code!"
task :one do

# ... do domething

ENV['PAR1'] = 'xxx'
ENV['PAR2'] = 'yyy'
Rake::Task[ "two" ].execute

# ... do something

end

the ENV assignments and execute call have a similar effect to executing in your shell:

rake two PAR1 = xxx, PAR2 = yyy

Yeah, Rake is a really easy and cool tool for every scripting need...

About Me

My photo
Hamburg, Hamburg, Germany
Former molecular biologist and web developer (Rails) and currently research scientist in bioinformatics.