Ruby binding: linklist collect method

added a collect method for freej's linked lists in the ruby binding.
Created an example script which uses this to print out a list of the
installed filters, sorted by name.
This commit is contained in:
Alex Norman
2009-06-22 12:49:48 -07:00
parent 251d788398
commit 3e2970caac
2 changed files with 31 additions and 0 deletions
+13
View File
@@ -51,5 +51,18 @@ module Freej
end
self
end
#apply the block to every element in the linked list and return an array
#with the resultant return values from the block
def collect(&block)
res = Array.new
index = 1
item = self.pick(index)
while item != nil
res << block.call(item)
index = index + 1
item = self.pick(index)
end
res
end
end
end
+18
View File
@@ -0,0 +1,18 @@
#!/usr/bin/ruby
#for now update the load path to include the location of
#the freej module and freej_extensions [as we haven't installed it yet]
$: << '../../bindings/ruby/.libs'
$: << '../../bindings/ruby/'
##import the Freej module
require 'Freej'
#initializes Freej creating a Contex
cx = Freej::Context.new
#refresh the filter list
cx.plugger.refresh(cx)
#we collect because this creates a ruby array out of the filter linked list
#then we sort by name [case insensitive], then we print the name and description
#of each of the sorted filters
cx.filters.collect{|f| f}.sort {|x,y| x.name.downcase <=> y.name.downcase }.each do |f|
puts f.name
puts "\t#{f.description}"
end