Open local HTML file in default web browser - ruby

IS there any way in Ruby to open a local HTML file in the user's default web browser? I could do something like:
system("open /path/to/file.html")
But that would only work on Mac OS X. Are there any solutions that work on any platform?

You can use the launchy gem.
First, install the gem:
$ [sudo] gem install launchy
Then, in your ruby code:
require 'rubygems'
require 'launchy'
Launchy::Browser.run("/path/to/file.html")

I think you will have to do system specific calls.
system() is like writing the command on the local system command line (that's my understanding anywayz)
I can't do what you have done on Windows 7. I have to call explorer and it opens in my default browser.
Windows 7 Example: (opens in Chrome, my default browser)
system("explorer file:///C:/path_to_file")
Note: I needed to put file:/// at the start otherwise it opens in Explorer instead of the browser.

I know it's not desirable, however worst case, you can check the value of RUBY_PLATFORM. This returns the operating system platform.
Semi-Pseudo Code:
cmd = case RUBY_PLATFORM
when /darwin/
"open /path/to/file.html"
when /windows/ #fix this, I'm not sure.
"explorer file:///C:/path_to_file"
else
"default"
end
system(cmd)

Related

How can I run a terminal command from a program?

I have a small ruby program that I'm working on at the moment (Pure ruby, not rails), which crawls a small portion of the web for certain movies. Is there a recommended way to, for example, when I run the script from my windows terminal, that it will open up a browser with the url that I extracted?
I know start http://example.com works from the windows terminal but im just wondering if there's a way to invoke a method like this directly from a ruby script.
I've found a nice gem called launchy to do this (at least opening a url)
http://www.rubydoc.info/gems/launchy/2.4.3
In the terminal :
gem install launchy
then:
require 'launchy'
Launchy.open("url")

Headless in Windows 7 for Ruby in Watir?

I am new here. Is there any way to use headless in windows? I tried but it says :
:/Ruby193/lib/ruby/gems/1.9.1/gems/headless-1.0.1/lib/headless/cli_util.rb:4:in ``': No such file or directory - which Xvfb (Errno::ENOENT)
Please help...
Headless Tests in "WINDOWS7" using WATIR? You can use phantomjs which is super easy to configure.
Follow these easy steps:
Download phantomjs from here
Extract to desired folder, once extracted you just need phantomjs.exe
Now where ever you instantiating the browser, use it like this:
phantom_dir = "location of phantomjs.exe"
ENV['PATH'] = "#{ENV['PATH']}#{File::PATH_SEPARATOR}#{ghost_dir}"
browser = Watir::Browser.new :"phantomjs"
To run headless on other OS follow easy steps here
You have to run it on a linux box. Xvfb doesn't exist in Windows or Mac

how to use RVM to a ruby app engine?

i wanna make a simple ruby App Engine in rails just like heroku, i'm dealing with a problem now.
My idea was:
1.use rails to establish the App Engine, use a class 'App' to handle all apps.
2.when a user create an ruby app he should offer it's git path
3.when the user deploys it, my app engine will do these things:
clone the git to a path in my server (done
use RVM to designatine the ruby version witch user wanted and make a gemdir for the project (some problems here
create a nginx conf for the project, then include it and reload nginx (i can do it
Problems in the second step:
codes here:
def start_thin
Dir.chdir(proj_path) do
system('rvm use ruby-1.8.7-p352#testname --create')
system('gem env gemdir')
success = system ('thin start -s3 --socket ' + self.proj_sock)
if success
return true
end
end
return false
end
when the code runs here, the log told me "RVM is not a function...blahblah", i know something about the login-shell and non-login-shell, then i try to fix it via editing .bashrc but same problem occurred.
And if i ignore it, the app can't be deployed, because of a Load Error :
myapp.rb:2:in `require': cannot load such file -- sinatra (LoadError)
if i open a terminal in that app directory, i can use thin to start it.
i wanna know how to run cmd just like in a terminal, without all these odd problem?
or how to edit my method to fix it?
Thanks!
Thanks Casper and GhostRider.
The user and rvm settings are correct.
After lots of googles and tests i found it's impossible...
Finally I fixed it by using RVM's ruby api instead of running system command.
Such as :
require 'rvm'
env = RVM.current
env.gemset.create('app1')

new window creation in watir

I am new to watir. I am trying to create new ie window with
browser = Watir::Browser.new
but it gives error message like
`user_is_bewildered': Error in the default values: :browser's value must be one of 'ie', 'firefox', or 'safari', and '' doesn't look right. (StandardError)
I donno how to set default browser. Can some one help me? There is a another thread here. But I am not able to understand what i need to do in ffi.
Thanks
If this is the original Watir gem, then the following is how I used to launch it:
require 'rubygems'
require 'watir'
Watir::Browser.default = "firefox"
browser = Watir::Browser.new
# Whatever you want to do in watir
IamChuckB's answer may be a more efficient way of doing this, but having not used it, I'm not sure.
You need to tell it which browser to open. Try this:
browser = Watir::Browser.new :ff
I haven't played around with watir since my last job so I had to look this up. As I last recall, WATIR was not entirely integrated with FireWATIR (the Firefox based variant). It's good to see that the two have apparently been reconciled in the meantime.
Taken from Watir in Five Minutes on Zeljko Filipin's github, BTW.
To install devkit,
create a folder in the ruby directory called devkit
get the devkit from here
unpack it into the devkit directory created in step 1
add c:\ruby193\devkit\bin;c:\ruby193\devkit\wming\bin to your path, of course adjusting for your ruby install directory
now open a command prompt to install the gem again
c:\> gem install watir
The issue is resolved. Thanx for the the inputs. The issue was with wrong nokorigi gem installation, initially i installed x86-mswin32-60, i uninstalled it and tried with x86-mingw32, it solved.

I can't install Haml/Sass on Windows using RubyInstaller for Windows

I never used ruby before, I just wanted to play around with HAML and SASS. I downloaded and installed Ruby's Windows installer (v1.9.1). Then, I clicked ruby.exe (the icon with a black window and a multicolored gem in the picture). Finally, I typed gem install haml and pressed Enter. But nothing happened. Am I doing something wrong?
Reference picture:
alt text http://img707.imageshack.us/img707/9863/haml.png
You might need to put the path to Ruby into the PATH environment variable to do this, but this is how I do it:
I open up the command line utility. I then type ruby -S gem install <whatever>. This works like a charm.
I tried running Ruby and it shows a blank screen but lets me type code. When I press CTRL + C to cancel it then executes my code as well. Maybe you need to do that in the manner you are trying to right now. I just find it easier to just ruby -S <statement> instead. IronRuby gives me the REPL no problems though.
ruby.exe is the Ruby interpreter. If you want to type code into it, you obviously need to type Ruby code into it, not DOS command code.
The gem command is a DOS batch file (gem.bat). DOS batch files need to be run from the DOS command interpreter.
Installing Ruby using RubyInstaller, you get an shortcut in the Programs menu that let you open a command prompt with Ruby in the PATH
You use that in case you didn't select the option to add Ruby to the PATH.
Either case, the gem command you typed in should be entered at the command prompt, and not inside Ruby itself.
The latest build (rubyinstaller-1.9.2-p136.exe) had a problem. Rename the folder: c:\ruby192\lib\ruby\site_ruby or delete it altogether and this fixes "gem"
You can do "gem install compass" or if you're behind a proxy you might need to do.
gem install –http-proxy compass
Here's a blog post with all the details:
http://francisshanahan.com/index.php/2011/how-to-theme-sencha-touch-sass-windows/
Hope that helps,
-fs
this is how i installed ruby and sass on my windows machine: How to install ruby and sass on windows?

Resources