SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
-
May 10, 2007, 20:28 #1
- Join Date
- May 2007
- Posts
- 5
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
from REALbasic > Terminal > RUBY ?!
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:
Code: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
Code:puts "Hello World!"
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?
-
May 11, 2007, 04:30 #2
ARGV is an array of the arguments you passed into a ruby script. So if you wrote this in your terminal:
Code:ruby say.rb hello john
Code:puts "#{ARGV[0]}, my name is #{ARGV[1]}"
Now, you can get fancier and do named arguments like:
Code:ruby say.rb --greeting hello --name john
-
May 11, 2007, 06:35 #3
- Join Date
- May 2007
- Posts
- 5
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Finally!!!
This simple test is working as follow:
>>>> REAL CODE <<<<
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
The ruby script [ruby-test.rb] puts both arguments into a single line as follows:
>>>> RUBY CODE <<<<
Code:puts "#{ARGV[0]} #{ARGV[1]}"
I'll have to learn about sockets.
Advices? Comments?
Bookmarks