SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Execute/Run the Ruby (.rb) files from browser.

    Hello ruby users,

    I am not new at all for ruby because i have studied for a month about ruby. Actually i need to create a heatmap (the overlay image that shows the click locations in my web page) for my site using ruby on rails which i got reference from http://blog.corunet.com/english/the-definitive-heatmap. And i am successfull to create heatmap in my local machine. Also i tried everything installing (ImageMagick, RMagick, Ruby as well as other needed software) and i am success now to create the images using RMagick/ImageMagick in the server from COMMAND LINE only. When i run the image creating code form command line as ruby something.rb. It reads the data from a text file and writes into a file that shows the click maps of my site. But when i run this same code from browser from a controller class and method, it does not work neither it gives me the error.

    And hereby now i want to know how to run or how to call the something.rb file which generates the image successfully from command line (SSH in PUTTY) from my controller class (calling method). How can we execute that .rb file from my ruby code (executing ruby files).

    I dont know whether i could make you understood my problem. Please help me. I am spending lots of days for this but i could not do.

    Thank you very much help in advance.

    Raju Gautam
    A New Ruby Programmer

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use ` (backtick) to execute programs:

    Code:
    puts `ls`
    This executes the ls-command and prints it's output.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean like this ?
    Code:
    class HeatmapController < ApplicationController
    	layout "standard-layout"
    	def initialize
    		require 'rubygems'
    		require 'RMagick'
    	end
    	def index
    	end
    	def write_click
                 #something to write into file
    	end
    	def WriteImage
                 `ruby /path/to/myfile/toexecute/file.rb`
    	end
    end
    Am I doing correctly to run/execute the file.rb while accessing http://www.mydomain.com/rails/heatmap/WriteImage ?

    This is not executing the file.rb at all. But when I run this file from command line as
    Code:
     ruby file.rb
    it does work properly.

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure that it isn't executing the script? The path may be wrong.

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2
    Are you sure that it isn't executing the script? The path may be wrong.
    Thank you for responding. I am sorry to say that its really not executing the script. I did as follows for proper ruby path :
    Code:
    class HeatmapController < ApplicationController
    	layout "standard-layout"
    	def initialize
    		require 'rubygems'
    		require 'RMagick'
    	end
    	def index
    	end
    	def write_click
                 #something to write into file
    	end
    	def WriteImage
                 `/usr/lib/ruby /path/to/myfile/toexecute/file.rb`
    	end
    end
    I did this with full path of ruby and my executing file.rb too. But its not being executed.
    Do i need any permissions to execute the files like that?
    Last edited by Raju Gautam; Dec 2, 2006 at 22:56.

  6. #6
    SitePoint Addict
    Join Date
    Mar 2004
    Location
    Grand Junction, CO
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use "load filename.rb" to load the file in.

    I think a much better bet would be to extract the script code into a class that does what you want. Then you can do something like

    Code:
    def write_image
      ImageGenerator.from_file 'your_text_file'
    end

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What about the path in ruby? When i load a file
    Code:
    load 'something.rb'
    it shows error in path of the file used in something.rb file. The relative path does not work in ruby when i run the site form browser. But it is ok while running form the command line.

    Please can anyone help on this matter too.

    With Regards
    Rajug
    Last edited by Raju Gautam; Dec 3, 2006 at 03:00.

  8. #8
    SitePoint Addict
    Join Date
    Mar 2004
    Location
    Grand Junction, CO
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug
    What about the path in ruby? When i load a file
    Code:
    load 'something.rb'
    it shows error in path of the file used in something.rb file. The relative path does not work in ruby when i run the site form browser. But it is ok while running form the command line.
    If you really want to go this route though, then I think you just have to use absolute paths instead of relative. In the file that you're loading, use

    require File.dirname(__FILE__) + '/some_other_folder/some_other_file'

  9. #9
    SitePoint Addict
    Join Date
    Mar 2004
    Location
    Grand Junction, CO
    Posts
    292
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, basically, that's just a crappy solution in the first place. As I mentioned, you're way better off encapsulating the behavior you want into a class of its own.


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
  •