How can I run a terminal command from a program? - ruby

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")

Related

How to start Rack application with frozen string literals?

I'm trying to run our application under ruby 2.3 using the new ruby feature for automatic frozen strings turned on globally. (Ruby 2.3) This is normally done by passing the argument to a ruby script when starting as follows:
ruby --enable-frozen-string-literal ruby_code_file.rb
Unfortunately, our application is started using rackup, and I've tried the following command:
rackup --enable-frozen-string-literal
But this does not appear to work. How do I pass that parameter into Rack?
You can't pass parameters for ruby to rackup, unfortunately. However, rackup is really, really simple:
#!/usr/bin/env ruby
require "rack"
Rack::Server.start
The simplest solution, then, is to duplicate this file in your project (in, say, bin/frozen_rackup) but change the first line to this:
#!/usr/bin/env ruby --enable-frozen-string-literal
Then make sure the file is executable (chmod u+x bin/frozen_rackup) and run bin/frozen_rackup instead of rackup.
P.S. I'm guessing that --enable-frozen-string-literal doesn't apply to gems your script requires, since it would break a lot of gems, but I haven't tested this and YMMV.

Executing a .rb file (Ruby file) with PRY in Windows 7

I was wondering how I can load a Ruby file into PRY to use the debugger function on my Windows 7 computer with Command Prompt. I want to use PRY to go through my program step by step and following the tutorials online on how to do it aren't working for me (probably because all of them are using Mac Terminal)
I use SublimeText 2 to write everything up and then save it as a .rb file and I was told that if I wanted to debug the file I just made, I need to run it through IRB or PRY. Not, I already ran "gem install debugger". And the Ruby I have installed is 1.9.3 - p545. Do I need to add an extra line of code that says "binding.pry" (I saw that in some of the samples that I could find on stackoverflow). All I'm looking for is a simple step-by-step process. Thank you for reading this, I look forward to the responses.
Make sure you've installed pry and pry-debugger gems
At the top of your file, add statements for require 'pry' and require 'pry-debugger'
In your code, wherever you want to start ddebugging, just add a statement binding.pry
Now, you can run your file like ruby filename.rb and the debugger should open

Need help with starting RoR: Command "rails server" does not result in "Booting WEBrick"?

I need help to get started with RoR.
I currently follow this guideline:
http://allaboutruby.wordpress.com/2009/07/20/installing-rails-on-windows-3-years-later/#comment-11099
I followed step 1 through 3 w/o problems.
In step 5: I can get the webserver through WEBrick working.
When i put
"rails server"
instead of getting "Booting Webrick", i get "rails new_path option"
thus when i try 127.0.0.1:3000 in the browser... it does work.
Can anyone guide me on this on how to get it up and runnning? (Im a total newb for now...so i need specific explanations! thanks!)
In your tutorial i can't see the command 'bundle install' - it's checking and installing all necessary gems in your system. So why you don't use another great rails tutorial - http://ruby.railstutorial.org/ruby-on-rails-tutorial-book
I'm guessing you are running windows, on which rails can be a little awkward. You'll probably need to run the rails server command by pointing ruby at the server script. On windows, your rails "commmand" is actually just a .bat file that lives in the /bin file of your ruby installation, and that .bat file just passes the arguments to ruby. If you look at the rails gem that is installed on your machine, you'll see the files that correspond to the normal first argument of a rails command (console, generate, server, etc). You might find it helpful to copy these to the /script directory of your application, and when you want to run a rails command you can just run "ruby script\server" from your application's main directory, though there may be more accepted ways of getting the same result.

Open local HTML file in default web browser

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)

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