
Here are few scripts which you will use most frequently. It depends mostly on what you're doing, how variable it is, and how frequently you're doing it. Ruby provides many useful scripts, which you use during your project implementation. Okay, so there are four different ways in which you can write and run scripts, so which one is best? Notice the only difference between this approach and #2 was that we don't need the ruby command in front. So, from your working directory, this would be: $. If you don't know what that means or if you know it's not in your path, you can now run the command, but must point to it relatively. Its easy to import and change data, because you can write your. If the bin is in your $PATH, you can run the script directly. Inside that task block, you can use all your models and the rest of the code in your Rails app. Now, before you are able to run the command, you'll want to make it executable. It's telling your shell in which language it should run the script, so it has to point to where ruby is installed on your machine. The top line in this file is called a Shebang. So, create your command-line script file and then add the contents: be ruby app/jobs/myjob.rb THERE ARE 2 USERS. Its a standalone Ruby utility that replaces the Unix utility make, and uses a Rakefile and. When I write scripts in this manner, typically I put them in a bin directory so they are all together and out of the way. Running rails runner with some parameters require optparseparser OptionParser.new do options options. Our last method is to actually write a command-line script so it behave like any other shell script. If you have rake installed, you can now run the script: $ rake hello_world This is because we have to tell Rake the name of our task (here that is hello_world). Notice there is more code in this method. Create a new Rakefile and add your script: Usually that file will load other rake files, but you can also author your commands directly in the file. When running rake within a directory, Rake looks for a Rakefile to know what to. While we can setup rake within a project in all sorts of ways, we're going to keep it simple for this example. Rake is a powerful build utility for ruby. To run the contents of the file, use the ruby command and pass in the path to the file: $ ruby hello_world.rbĪ fairly common place to put ruby scripts is inside a rake task. It ships with all the tools needed to build amazing web apps on both the front and back end. And you can save it to your current working directory. In the second approach, to author we simply write our script to a ruby file. (Type exit to get out of the session.) 02: Ruby File Your prompt will change and you can author ruby directly: irb(main):001:0> puts 'Hello World' You can do this from any directory and the ruby console will maintain a reference to knowing in which directory you are working. This will look familiar if you've ever worked with Rails because this is what the Rails console uses. IRB stands for "Interactive Ruby" and opens up an active ruby session.Īll you have to do is run irb and you'll be placed into an interactive ruby session.

This will look familiar if youve ever worked with Rails because this. But know that this is running ruby code, so you can write any ruby code in the place of this code and it should work to your benefit! All you have to do is run irb and youll be placed into an interactive ruby session. As the simplest form of a script, we're only going to print "Hello World" to the console. The Scriptīefore we get into the methods, let's look quickly at the script. And when we're done, we'll talk about when to use which method. Using the classic "Hello World" script (where all we do is print "Hello World" to the console), I'm going to show you four ways in which you can write and run a script with Ruby. From little helpers to full-on command-line utilities, the possibilities are endless. on '-t', '-the-arg SOME_ARG', 'Shows that we can take an arg' do |arg| puts "THE ARGUMENT WAS # USERS" # I have a users modelĬalling with no args: $ be ruby app/ jobs/my_job.rb THERE ARE 2 USERSĬalling with an arg shorthand: $ be ruby app/ jobs/my_job.rb -t my_argTHE ARGUMENT WAS "my_arg"THERE ARE 2 USERSĬalling with an arg long-hand: $ be ruby app /jobs /my_job.One-off ruby scripts run the gamut on the services they can provide you. Then you can just write your scripts in there.įor example, I have a script that takes an argument, prints it out if it was provided, and then prints out how many users are in my app:įile: app/jobs/my_job.rb require 'optparse'parser = OptionParser. I assume you're on an older Rails based on script/runner I don't know if this works for older Rails' or not, but in newer Rails, you can just require 'config/environment', and it will load the app.
