PDA

View Full Version : from REALbasic > Terminal > RUBY ?!


gcarcass
May 10th, 2007, 11:24 PM
Hello:

This might seem weird, I just started Ruby, so don't know much about it, HOW CAN I PASS ARGUMENTS AS A STRING FROM A UNIX TERMINAL INTO A RUBY SCRIPT?

I'm developing an application with REALbasic(why?!, because I already own a license and with REALbasic I can develop and application that can run natively on OS X without the need of installing extra libraries(wxCode, wxRuby, etc(problems with building and deploying, of course I would have to include the script file, but that's simpler no?)) and using just the Ruby language installed by default on OS X), I would like to use also RUBY to process ALL the text that comes from Editfields in REAL.

I already did this "hello world" test and it's working, at least one way. In this sample you have a window, an editfield and a pushbutton, once the pushbutton is pressed, it executes a rubyscript and return its result to be displayed on the editfield. The REAL code is:

Dim s as New Shell
Dim cmd as String
#if TargetMacOS or TargetLinux and Not( TargetMacOSClassic)
cmd="cd /Applications/hello-ruby ; ruby hello.rb"
#elseif TargetWin32
cmd="set"
#endif

s.execute cmd
if s.errorCode=0 then
EditField1.text=s.Result
else
EditField1.text="Error "+ Str(s.ErrorCode)
end if


If you are not familiar with REAL, in this sample I'm using a SHELL class that can be used to execute Unix or DOS shell commands under Windows, Mac OS X, or Linux, so in the OS X terminal runs [cd /Applications/hello-ruby ; ruby hello.rb] and inside the ruby script there is a simple

puts "Hello World!"

which is returned to the editfield in the REAL interface and displayed. NOW! I would like to make another sample that could work in the other way also, so let's suppose I have 3 editfields and a pushbutton, in editfield1 I enter "hello", next in editfield2 "world!", once the button is pressed the editfield3 displays the result "hello world!" which was concatenated in a ruby script.

From the Shell class of REAL I would have to send the strings into the terminal using ECHO, then How I pass them into the ruby script from the terminal to be processed?

gcarcass
May 11th, 2007, 09:33 AM
Finally!!!

This simple test is working as follow:

>>>> REAL CODE <<<<

Dim s as New Shell
Dim cmd as String
Dim t1 as String
Dim t2 as String

t1 = EditField1.Text
t2 = EditField2.Text

#if TargetMacOS or TargetLinux and Not( TargetMacOSClassic)
cmd="cd /Applications/hello-ruby ; ruby ruby-test.rb " + "'"+t1+"'" + " " +"'"+t2+"'"
#elseif TargetWin32
cmd="set"
#endif

s.execute cmd
if s.errorCode=0 then
EditField3.text=s.Result
else
EditField3.text="Error "+ Str(s.ErrorCode)
end if


Yeah I know the multiple quotation marks looks awkward in the cmd... line but it was the only thing that I came up with to allow the terminal passing the t1 and t2 strings when these have have more than one word(spaces in between).

The ruby script [ruby-test.rb] puts both arguments into a single line as follows:


>>>> RUBY CODE <<<<

puts "#{ARGV[0]} #{ARGV[1]}"

I know this is simple (many things are missing) and that REALbasic can concatenate, but! this is just a test. I dunno how REAL or RUBY are gonna behave with a more complex example, but it's a beginning!!

I'll have to learn about sockets.

Advices? Comments?

rob
May 13th, 2007, 06:30 PM
Sorry I didn't see this earlier,

The ARGV array contains your command line arguments. If you want to step through the arguments, you can use:

ARGV.each { |a|
if a == "whatever" .. etch
}

Using this idiom, you can check all elements for certain items.

gcarcass
May 13th, 2007, 09:06 PM
Thanks a lot!!!