SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    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!"
    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?

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    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
    Your script should look like this to make use of the two arguments:
    Code:
    puts "#{ARGV[0]}, my name is #{ARGV[1]}"
    When you run the script with those arguments, it would return the string "hello, my name is john".

    Now, you can get fancier and do named arguments like:
    Code:
    ruby say.rb --greeting hello --name john
    This way your arguments don't have to be in any particular order for your script to run properly. To do this you'll need to learn about the optparse library.

  3. #3
    SitePoint Member
    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
    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 <<<<

    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?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •