Forum: Ruby from REALbasic > Terminal > RUBY ?!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
42fab8b68784ff66159ef41595b03c13?d=identicon&s=25 Watanabe Carcass (gcarcasss)
on 2007-05-11 05:28
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)) 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?
Df13d29fbc02d6979b4e6d3a3ccb4e4a?d=identicon&s=25 Bino Oetomo (bino)
on 2007-05-11 05:49
Watanabe Carcass wrote:
> 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 did this ... well not a topnotch one.
I'm playing with ttyctrl --> http://ttysctrl.sf.net
This "small" utility will read the status of COMport and do some defined
action ... and I want that this "action" recognized by a runing ruby
script.

For this ... I make a temporary file in my ramdisk ..
Tell ttyctrl to write a string based on witch button-press read by it.
In a noher process, I have a running ruby script that loop to read the
file above .. and do further action based on it content.
In My case .. the ruby script will send content of that file to another
process in another mechine via XMPP (here is where XMPP4R do the trick)
I'll use same trick to send NMEA sentences from one mechine to another
for further process (Map ?)

But .. I think if you have better knowledge than me ... you can use
RubyD
Or basicaly ... let that two application talk eachother via unix socket.

Sincerely
-bino-
42fab8b68784ff66159ef41595b03c13?d=identicon&s=25 Watanabe Carcass (gcarcasss)
on 2007-05-11 06:03
> But .. I think if you have better knowledge than me ... you can use
> RubyD
> Or basicaly ... let that two application talk eachother via unix socket.

Certainly don't have better knowledge, I have no idea what you are
talking about, je je je.
Something simpler to understand...?

Thanks anyway for your reply!!
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2007-05-11 06:21
(Received via mailing list)
On Behalf Of Watanabe Carcass:
# HOW CAN I PASS ARGUMENTS AS A STRING FROM A UNIX TERMINAL INTO A RUBY
# SCRIPT?

this is just one simple stupid example. there are many ways.

root@pc4all:~# uname
Linux

root@pc4all:~# cat test.rb
x=gets
puts "you passed: #{x.upcase}"

root@pc4all:~# echo this is a test | ruby test.rb
you passed: THIS IS A TEST
42fab8b68784ff66159ef41595b03c13?d=identicon&s=25 Watanabe Carcass (gcarcasss)
on 2007-05-11 06:25
Peña, Botp wrote:
> On Behalf Of Watanabe Carcass:
> # HOW CAN I PASS ARGUMENTS AS A STRING FROM A UNIX TERMINAL INTO A RUBY
> # SCRIPT?
>
> this is just one simple stupid example. there are many ways.
>
> root@pc4all:~# uname
> Linux
>
> root@pc4all:~# cat test.rb
> x=gets
> puts "you passed: #{x.upcase}"
>
> root@pc4all:~# echo this is a test | ruby test.rb
> you passed: THIS IS A TEST

Thanks!! it may be just what I needed.
Df13d29fbc02d6979b4e6d3a3ccb4e4a?d=identicon&s=25 Bino Oetomo (bino)
on 2007-05-11 06:35
Watanabe Carcass wrote:

> Certainly don't have better knowledge, I have no idea what you are
> talking about, je je je.
> Something simpler to understand...?
>
> Thanks anyway for your reply!!

Honestly .. I also a dumb newbie in ruby world .. thats what I mean..
I Assume you played with REAL quietwell .. and I hope you can do socket
programming with it.
If So .. you will be easy to learn to write socket programming with
ruby.
Here is a realy good simple start point for it :

http://sitekreator.com/satishtalim/socket_programming.html

Thanks to you .. your question make me find this url .. and thinking to
switch my curent methode (mentioned in previouse post) to socket
methode.

Somehow we share the same "dream"

Sincerely
-bino-
1c0cd550766a3ee3e4a9c495926e4603?d=identicon&s=25 John Joyce (Guest)
on 2007-05-11 06:40
(Received via mailing list)
On May 11, 2007, at 1:25 PM, Watanabe Carcass wrote:

>>
> Posted via http://www.ruby-forum.com/.
>
You could also simply create a temp file or some kind of data store
to share.
42fab8b68784ff66159ef41595b03c13?d=identicon&s=25 Watanabe Carcass (gcarcasss)
on 2007-05-11 06:55
Bino Oetomo wrote:
> Honestly .. I also a dumb newbie in ruby world .. thats what I mean..
> I Assume you played with REAL quietwell .. and I hope you can do socket
> programming with it.

>
> Somehow we share the same "dream"
>
> Sincerely
> -bino-

Actually I just know the basics of REALbasic and Ruby, but I started to
wonder about "things that seem obvious", maybe they are not. But If
"Ruby is a scripting language" and REALbasic can call scripts, why not
give it a try. Maybe is just nonsense, but you never know.

I'm not a programmer, my relation with computers is far away from being
it, but maybe this "ignorance" gives me a new approach.

Yeah, maybe we share the same dream.
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-05-11 08:55
(Received via mailing list)
On Fri, May 11, 2007 at 12:28:15PM +0900, Watanabe Carcass wrote:
> 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?

For small arguments you can stick them on the command line, and they
will be
available in the array ARGV. But if those arguments contain spaces
you'll
have to be careful with quoting.

$ cat x.rb
puts "arg 0 = #{ARGV[0]}"
puts "arg 1 = #{ARGV[1]}"
$ ruby x.rb foo "bar baz"
arg 0 = foo
arg 1 = bar baz
42fab8b68784ff66159ef41595b03c13?d=identicon&s=25 Watanabe Carcass (gcarcasss)
on 2007-05-11 15:37
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?
This topic is locked and can not be replied to.