Utviklingsavdelingen i PSWinCom har akkurat begynt å lære seg Ruby, og jeg har derfor kodet noen scripts som jeg har dokumentert ganske grundig - sånn at det går an å lære noe av å lese dem. Dette er et faktisk IronRuby-script jeg bruker for å slette eller tømme alle private MSMQ-køer på lokal maskin (vi jobber mye med køer, og trenger ofte å rydde opp miljøet).
Så i tilfelle dette kan hjelpe andre – her er koden:
1 =begin 2 This is a comment :) 3 4 Ruby scans scripts from top to bottom. This means it should make 5 sense when you to read it that way too. 6 7 The scull and bones art is contained in something called a 8 here-document. Google it if you want to know more... 9 10 (I had to escape the backslashes, so it looks a bit off in the 11 source, but the output should be nice.) 12 =end 13 puts <<EOF 14 _______________ 15 / \\ 16 / \\ 17 / \\ 18 | XXXX XXXX | 19 | XXXX XXXX | PSWinCom MSMQ IronRuby script 20 | XXX XXX | 21 | X | ----------------------------- 22 \\__ XXX __/ 23 |\\ XXX /| A L L Q U E U E S 24 | | | | 25 | I I I I I I I | ----------------------------- 26 | I I I I I I | 27 \\_ _/ Hope you know what you're doing 28 \\_ _/ 29 \\_______/ 30 XXX XXX 31 XXXXX XXXXX 32 XXXXXXXXX XXXXXXXXXX 33 XXXXX XXXXX 34 XXXXXXX 35 XXXXX XXXXX 36 XXXXXXXXX XXXXXXXXXX 37 XXXXX XXXXX 38 XXX XXX 39 40 EOF 41 42 =begin 43 The Actions class contains a method for each of the available 44 command line options. To add another option it should be enough 45 to expand this class with a new method. 46 =end 47 class Actions 48 # You create "static" methods (we actually call it class methods) 49 # by applying 'self.' in front of the method name. Here self refer 50 # to the class, and it could also be written ClassName.method_name. 51 def self.list queue # method named "list", takes one argument "queue" 52 puts queue.path 53 end 54 def self.delete queue 55 puts "Deleting #{queue.path}.." 56 MessageQueue.delete queue.path 57 end 58 def self.purge queue 59 puts "Purging #{queue.path}.." 60 queue.purge 61 end 62 def self.exit 63 puts "Usage: ir all_queues.rb action" 64 puts " where action in [list|delete|purge]" 65 Kernel.exit(0) # This is a nice way of ending a script 66 end 67 end # class Actions ends here.. 68 69 =begin 70 The rest of the script is not contained in a class. You may feel dirty 71 the first couple of times you do this, but it's fine - I promise! 72 =end 73 74 mode = $*.shift # get the first command line option passed to this script 75 Actions.exit unless mode # call exit if no mode (if mode == nil) 76 77 =begin 78 Load the .Net dll. 79 This is the only line of the script that will look strange to a normal 80 Ruby-developer that haven't used IronRuby. 81 =end 82 load_assembly 'System.Messaging' 83 84 =begin 85 By including the System::Messaging namespace in this script, I'm able to 86 use the classes in the namespace directly. If I hadn't done this I would 87 have to use System::Messaging::MessageQueue instead of just MessageQueue. 88 89 Notice that when Ruby was parsing the Actions class, which use the 90 MessageQueue class, the System.Messaging.dll was not jet loaded, and the 91 namespace not yet included. That is fine however - when actual execution 92 enters the Actions class's queue methods (when the methods are called), 93 the namespace will be there. 94 =end 95 include System::Messaging 96 97 =begin 98 Getting queues. MessageQueue.get_private_queues_by_machine in ruby corresponds 99 to MessageQueue.GetPrivateQueuesByMachine in C#. 100 =end 101 all_queues = MessageQueue.get_private_queues_by_machine('.') # '.' == local machine 102 103 # Let user know if there are no queues to perform action on 104 puts "No queues available" if all_queues.length == 0 105 106 =begin 107 The following line is very similar to C#'s List<T>.ForEach(Action<T> action). 108 For each queue I call the appropriate action. I do this by sending the 109 command line option (mode) as a message to the Actions class. The method 110 with the correct name will be called. The second argument will become the 111 argument to the actual method. 112 =end 113 all_queues.each {|queue| Actions.send(mode, queue) } 114 115 puts '.' # Just an end-of-program indicator
For å for eksempel liste alle køene eksekverer man "ir all_queues.rb list" i kommandolinjen (all_queues.rb er navnet på skriptet og ir er IronRuby). Skriptet benyttes på egen rissiko, jeg tar ikke ansvar for tapte data eller andre problemer som måtte oppstå ;)