Hi there, i''m wondering if it''s possible to execute a external Ruby-Script via Rails. It is my aim to scan my hard-disc with this script, returning values (my directory-structure) to rails. Can anyone give me a hint? -- Posted via http://www.ruby-forum.com/.
On Tue, Jul 14, 2009 at 1:49 AM, Hans Hartmann < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi there, > > i''m wondering if it''s possible to execute a external Ruby-Script via > Rails. > > It is my aim to scan my hard-disc with this script, returning values (my > directory-structure) to rails. > > Can anyone give me a hint?Yes, it''s possible to execute a Ruby script from Rails. Please remember that Rails is implemented in Ruby and you can use an method called ''system''. Good luck, -Conrad --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi there, now i tried to execute via System-Command but i just can catch the value true/false. Is it possible to declare a global variable in my ruby script - execute the script - and then catch it with rails? Greetz Hans -- Posted via http://www.ruby-forum.com/.
On Tue, Jul 14, 2009 at 3:04 AM, Hans Hartmann < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi there, > > now i tried to execute via System-Command but i just can catch the value > true/false. >Are you doing something like this? exit_status = system( <some_command>)> > Is it possible to declare a global variable in my ruby script - execute > the script - and then catch it with rails?Yes, it''s possible to declare a global variable within your script. I would recommend getting yourself a copy of "Programming Ruby" as a desk-side reference. Good luck, -Conrad --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I try and try... but I can''t make the script be executed (nor an other Programm) Can someone give me an example to execute external files with rails? So I can get an idea of the path-description. Thx a lot -- Posted via http://www.ruby-forum.com/.
Conrad Taylor wrote: [...]> Yes, it''s possible to execute a Ruby script from Rails. Please remember > that Rails is implemented in Ruby and you can use an method called > ''system''.If the external script is in Ruby, then it probably makes more sense to use require and load it as part of the Rails app, rather than using system to call it. You''ll get better integration and not have to spawn a separate shell.> > Good luck, > > -ConradBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
On Jul 14, 7:31 am, Hans Hartmann <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I try and try... > but I can''t make the script be executed (nor an other Programm) > Can someone give me an example to execute external files with rails? So > I can get an idea of the path-description. > > Thx a lot > -- > Posted viahttp://www.ruby-forum.com/.What you are looking for is the backtick ''`'' method of Kernel, aliased as %x(). You should refer to the official api at http://ruby-doc.org/core/ and read this carefully. Rails is a framework not a programming language. So it is really a default directory layout supported by a vast amount of clever code to do most of the grunt work required to write web apps. As other have pointed out, Ruby is the language used. To run one Ruby script from another your would have something that looked like this, more or less. caller.rb #!/usr/bin/env ruby # call the other script and open an new shell %x(path/to/my/other/script.rb) #eof script.rb #!/usr/bin/env ruby puts "script.rb has been called" #eof Now, this will display nothing because the stdout of the target script is not looked for in the calling script, which is the one you ahve open in your console. So: caller.rb #!/usr/bin/env ruby # call the other script and open an new shell my_stdout = %x(path/to/my/other/script.rb) puts my_stdout #eof That should display the output of script.rb on the console running caller.rb. script.rb''s stderr can be redirected as well: caller.rb #!/usr/bin/env ruby # call the other script and open an new shell my_stdout = %x(path/to/my/other/script.rb 2> &1) puts my_stdout #eof HTH
James Byrne wrote: [...]> What you are looking for is the backtick ''`'' method of Kernel, aliased > as %x().[...] This shells out just like system() does. It''s a good general method of running arbitrary shell scripts in other languages, but it should not be necessary when the scripts are in Ruby. Integrate instead of shelling out. [...]> To run one Ruby script from > another your would have something that looked like this, more or less. > > caller.rb > #!/usr/bin/env ruby > # call the other script and open an new shell > %x(path/to/my/other/script.rb)No. You''d be better off using require or similar. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser wrote:> James Byrne wrote:> > No. You''d be better off using require or similar. >That really depends upon the question being asked. The OP explicitly asked on how to run an external script, that happened to be written in Ruby; not, what is the best way to run arbitrary external ruby code from inside a running Ruby script? If the latter case then I suggest that the OP consider this approach, which was recently shown to me by Matt Wynne on the Cucumber list: First, write the script and encapsulate its entire code within an execute method contained in a Script class file. Like so: script.rb class Script # should be the actual name of the script. def initialize(argv) # receive the ARGV array from the calling process @argv = argv end def execute # if required then parse any command line arguments in @argv here puts "script.rb called" end end #eof If a stand-alone script initiation is required, as with cron say, then write a stub script that calls the script class and executes the script code like so: runner.rb #!/usr/bin/env ruby require ''script'' Script.new(ARGV).execute #eof If the encapsulated script is complex enough that it contains its own internal classes then the whole can be encapsulated inside an outer class given the script name. The inner class containing the execute method is then renamed to something like Main. In this case the call becomes: Script::Main.new(ARGV).execute And the class file script.rb has this structure class Script class Main def initialize(argv) @argv = argv end def execute script code goes here end end class OtherClass ... end end One may now use the script within any other ruby process as you suggest, via a simple require and subsequent instantiation. I found this technique extremely valuable in testing since out of process scripts did not share the dbms state with the the test harness. -- Posted via http://www.ruby-forum.com/.