I have developed a liking to Ruby since I first started using it for automation and now I even started to use it at work for one conversion process. Unfortunately, there are some set backs I've found with Ruby, such as using the Time class. I think my disliking of using it is probably because I've been spoiled with the well developed C# one. It lacks a lot of useful things. However, since Ruby is dynamic, I could make some of the necessary changes myself.
I had to do some extensive operations with the Time class and even though it had a lot of features, it lacked the ones I used the most. It got so bad that at a point, I had to start using Ruby's method extension feature which helped to solve my problem to a great extent.
I would however, like to ask the Ruby developers to clean up their Time class (I am not the only one saying that, just do a google on Ruby's Time class and see what I mean).
To insert class methods is easy. Let me show you:
class Time
class << self
def some_method()
end
end
end
So now I can do this: Time.some_method()
This allows me to insert custom Time creation methods where I can specify a time zone offset along with the time data I want.
What about some instance methods? How can I add for instance a date() method where I can tell Time.now to just give me the date component? Adding a class method won't help here. This requires a little hack (in my opinion).
First the Module:
module TimeInstance
# Converts the time to UTC and converts it
# into epoch in milliseconds
def to_epoch_millis()
(to_f() * 1000.0).to_i()
end
# Gets only the Date component of the current
# time instance
def date()
Time.local(year, month, day, 0, 0, 0, 0)
end
end
And in the Time class:
class Time
...
class << include TimeInstance
end
end
Now I can say: Time.now.date()
Now whatever you add in the TimeInstance module, will be added to the Time class's instance method list. The only catch is, you can only call public methods from the Time class in your module.
But this solves my problem greatly.
Hope this has helped you.
Thursday, March 19, 2009
Ruby class method extensions
Posted by LA at 9:33 AM 1 comments
Friday, January 30, 2009
OpenDialog in Ruby
I have encountered some issues trying to get Ruby to open up an Open Dialog Box.
I tried doing it the DL route but apparently there is some problem with Ruby properly creating the struct to be passed to the 'GetOpenFileNameA' function (at least, that's what I've encountered). But I have found a different way...
Fortunately for me, I have delved into AutoIT scripting which allows me to create an Open Dialog Box. So I then decided to write a simple script to pass all the necessary params to this application from Ruby (i.e. Dialog message and filter) and the AutoIT application returns the filename that is selected or nil if canceled.
AutoIT code:
If $CmdLine[0] < 1 Then
ConsoleWriteError("No arguments given")
Exit
EndIf
$op = $CmdLine[1]
Switch($op)
Case "open"
If $CmdLine[0] < 3 Then
ConsoleWriteError("open command requires message and filter arguments.")
Exit
EndIf
$msg = $CmdLine[2]
$filter = $CmdLine[3]
$selected = FileOpenDialog($msg, "", $filter, 1)
If @error Then
ConsoleWriteError("Open Dialog problem")
Exit
EndIf
ConsoleWrite($selected)
EndSwitch
Ruby code:
f = IO.popen("controls.exe open \"Select a file\" \"Text File(*.txt)\"")
puts f.gets
This seems to work nicely. A bit of a round-trip but allows you to get what you need.
For those who don't know, AutoIT is a Windows-based automation scripting system (console and GUI wise, see some of my AutoIT posts).
Cheers!
Monday, December 29, 2008
Line Control
Man, ever since working with Open SuSE, I always wanted a solution that could allow remote clients to let a (non SuSE) Linux Gateway dis/connect from/to the internet. Thankfully I found a solution for Ubuntu that works just as well.
I don't know how many people who have a Linux Gateway have a need for something like this but I wondered if I would able to find something like this.
Let me first explain what I am blabbering on about.
I have multiple home PCs and each want internet access. Unfortunately my internet device is a USB one so I can't buy a router and use that, so I needed a PC that was dedicated to be on the internet when I want it. I didn't want the internet to be online 24/7 for security reasons and also connection stability reasons (sometimes the connection quality drops and so a quick disconnect & reconnect sometimes solves this problem).
OpenSuSE had a SMPPPd solution where you have KInternet clients (where KInternet is so far only a SuSE app) that connects to a SuSE Gateway which is running SMPPPd that handles the internet connection. Once a client has connected to that Gateway, s/he can control the internet connection and see what the traffic looks like in KB/s. The problem with this solution is... It's only for SuSE! So I needed something similar for Ubuntu.
The solution I found was by accident as one can see in this forum thread: Visual Real-time traffic monitor
I was looking for a realtime traffic monitor and hoped to just write my own app that can deal with the connections but thanks be to God (I have prayed for something like this, where I asked God to help me get this Gateway back up), I stumbled on an client app called QLineControl which is a Qt based app for Windows and Linux that acts as a client for Line Control. You can download the Windows version and you can get an "unstable" version from apt-get by adding the repository listed on that website.
From there I was led to LineControl (the server is called LineSrv) which is the Linux service which actually controls the internet connection. LineSrv (so far) is listed as an official Ubuntu package which you can get from apt-get.
It took a while to figure out what to do but thankfully I managed to get it to work.
The config file generated by the apt-get for LineSrv is faulty so you have to override with the default config file and go from there.
Here is what I did:
I copied the /usr/share/doc/linesrv/linesrv.conf to /etc/linesrv/.
Then I created 2 scripts for dialing up and disconnecting:
Dialup script#!/bin/sh
ifup ib0 2> /dev/null > /dev/null
Hangup script#!/bin/sh
ifdown ib0 2> /dev/null > /dev/null
I placed them in the /etc/ppp path.
The ib0 is my iBurst (internet device) interface, I have to do it like this otherwise it won't work, I am using the Debain dialer. Also LineSrv has to be root for this to work. Fortunately the init script that is setup via apt-get does that automatically.
I know the script looks funny but this is the script format that was followed in the sample scripts found in the LineSrv source files.
Then I modified the /etc/linesrv/linesrv.conf file:
And then I ran the LineSrv service and there you go. I connected with QLineControl just fine without any hassles.
And now I am writing this post from this very configuration within Linux. I am quite surprised that such a solution even exists as I really thought that I would have to write my own app.
Well, there you go!
Cheers and God Bless!
Posted by LA at 5:52 PM 0 comments
Keywords: admin,line control,linux,software
Saturday, December 06, 2008
How will I go with this?
Yeah, how will I go with this blog?
Its a difficult time for me to determine how this blog is going to head. The last entry was 8 months ago because I lost interest in blogging.
Sometimes I do have some topics to write about but end up discouraged to do so via lack of time or fatigue. You know how it is, right?
So just to let everyone know, I am not sure when my next entry is going to be or whether this blog is going to continue to be updated. It will however remain up for people to read.
I also fixed the tag image issues that I had with the Digg and Delicious tagging.
All I can say is, check in once a week if you want to or subscribe this blog to your RSS reader to detect new additions. I am sorry folks, I am trying. :)
I am sure God will lead me with this as well. Maybe I might start a new blog, who knows!
Posted by LA at 9:25 PM 0 comments
Friday, May 09, 2008
Batch Scripting Tips
Here are some BATCH script tips which I discovered on the web that helped me a lot in Windows.
Why did I use BATCH scripts? Why not Ruby or Python, or even JScript? Well, if you're writing some basic automation which should be run on deployment PCs, etc. BATCH files are standard and sufficient enough to work. Installing Ruby, etc. on all the machines first is a pain and an unnecessary step (IMO).
I am keeping this for future reference. There is a website that is dedicated to this kind of thing: http://www.robvanderwoude.com/
Now I assume you have basic BATCH scripting knowledge.
Hiding Commands
If you want to hide all the ECHO and COPY, etc. commands from being displayed, insert this in the top line of your script file:@ECHO OFF
Printing an Empty line
Sometimes you want to produce an empty line in the console output. This can be accomplished by:ECHO.
Note: The . (DOT) is directly after the ECHO command and has no spaces in between!
Variables
If you have a variable to hold certain values, to set it you type:SET MYVAR=SOME VALUE
You would preferably type the value without quotes, since the quotes will be included in the value.
Using a value:ECHO %MYVAR%
Which should output:SOME VALUE
User input
If you would like the user to type in something, you use the /P option of the SET command:SET /P CHOICE=Type a letter and ENTER:
This will output:Type a letter and ENTER:
Now the user can type in something and press ENTER. This will store the typed in text in the variable CHOICE.
Substring
If you want to take a substring from a variable, you can do so by:%CHOICE:~0,1%
This will take the 'User Input' sample's CHOICE variable's 1st character. As you have noticed, it starts with the variable's first index (0) and by using length (1), like in Java's substring() method, you will get the first character of the variable.
If statements
Now, if you want to do a check by comparing a variable's value by a certain string, you can make use of the IF command.IF /I '%CHOICE%'=='A' GOTO CHOICEA
IF /I '%CHOICE%'=='B' GOTO CHOICEB
CHOICEA and CHOICEB are just labels to where the batch process should jump to if the given conditions are true. It doesn't have to be GOTO statements, it can be single statements, such as ECHO or SET. The /I option tells the IF statement to do a case-insensitive comparison. If you compare your variable against a string, you have to place it inside quotes, like above.
Labels
A label is a place to where the batch processor jumps to after executing a GOTO statement. Example::CHOICEA
Current path variable
This will output the current directory:ECHO "%CD%"
Path of batch file
Sometimes you want to work relatively from the path of the batch script file, rather than the current directory.ECHO %~dp0
This will output the path of the batch file that is running.
Posted by LA at 12:57 PM 0 comments
Keywords: automation,BATCH,scripting,tasks
Thursday, May 01, 2008
Ruby and Win32 API
If you are using Ruby as an automation process rather than a development tool, you sometimes might want to use some of Windows' GUI controls, such as a message box or folder browser box. In those cases, you might want to try the DL library.
Some of you have read this post about OLE Automation and DL. In a way it is quite simple but it can take a while to get right, especially if you're a beginner like me. However, after some time I have managed to get my folder browser to work and I haven't found Ruby code on the web that does this yet.
However, I feel like it is rather simple to do and to find out how to make your own version, all you need to do is to see how people who wrote VB code do it.
Why? Because it is that straight forward, in fact, Ruby reduces the amount of lines of code to do it.
I must warn and say that doing this kind of programming requires excessive use of the Win32API docs (one form or another) and some skilled programming, especially in the C-language area, because you are interfacing to the actual C-functions from Ruby.
Many VB code snippets that does this, have referenced these 2 c-functions (from the shell32 DLL):
They also require a structure to send data to the BrowseForFolder function.
Here is a site to get you started.
Why don't I just paste the code? Well, actually the method I wrote to bring up the dialog box, requires a whole Module and I would like to write my own Ruby module before I publish anything.
Yeah, I might sound like a bugger but hey, I have posted lots of lines of code on this site before so... take it like a man! ;)
The hardest part is creating a Ruby C-like structure from the VB structure, so I think I'll help you out on that one:
ptr = DL.malloc(DL.sizeof('LLSSLLLL'))
ptr.struct!('LLSSLLLL', :br_hOwner, :br_pidRoot, :br_displayName, :br_title,
:br_flags, :br_fn, :br_lparam, :br_iImage)
Now don't say I didn't give you anything! :D
For those who still don't know what all those L's and S's are for, it is basically saying "This is a Long integer type" or "This is a string type". Its to determine the size of the structure, but you know that there are other data-types so you need to dig in on your own regarding that.
Now there is one thing I'm having trouble with and it is the Open/Save File Dialog box. Aparently if you take the exact same data-types as the prescribed docs say, Ruby fails to bring it up. After hours of struggling, I decided to skip the VB code and look on the web if someone already did this, and to my "surprise", nobody has posted any code on this. Bummer! So I decided to have a look on the MSDN site, and BAM! I found my answer. The reason why Ruby didn't bring up my dialog box was due to the fact that Ruby created a structure that had a different size than what the function was expecting! So now I am trying to find out where, how and why.
I might post my code and findings at a later stage, for this is a necessary thing for Windows Ruby Automation Programmers, like me! ;)
Posted by LA at 8:43 PM 0 comments
Keywords: automation,productivity,programming,ruby,scripting,tasks,windows
Wednesday, April 30, 2008
My "facial expression"
Someone sent me this link which I rather think is funny.
Computer languages and facial hair
Hmmm... good thing I'm growing mine! :D
Posted by LA at 5:41 PM 0 comments
Keywords: funny
Netbeans 6.1
As if Netbeans 6.0 wasn't enough to patiently wait for (not in a bad way), the new 6.1 is ready for download/order!
Check this link out to find out more of what they did in this version.
Posted by LA at 5:25 PM 0 comments
Keywords: programming,software



