How to run Ruby scripts in Command Line? - ruby-on-rails

I've installed Jekyll and I'm trying to import a simple CSV file with a few posts. I've never used Ruby before. In Jekyll's import docs it shows the following code that needs to be run via Command Line.
http://import.jekyllrb.com/docs/csv/
$ ruby -rubygems -e 'require "jekyll-import";
JekyllImport::Importers::CSV.run({
"file" => "my_posts.csv"
})'
I'm I supposed to type the whole thing into Command Line and run it or is the first line supposed to call a function that includes the last three lines of code?
Sorry for the noob question but I really couldn't find any answers to this.
Thanks

You can put scripts in a file with a .rb extension and run it with:
ruby import_csv.rb
Your file import_csv.rb would contain:
#!/usr/bin/env ruby
require "jekyll-import";
JekyllImport::Importers::CSV.run({
"file" => "my_posts.csv"
})
I think the -rubygems switch is not needed since it is enabled by default. If not, add the line require rubygems before the other require line.

Just put your codes in a file with rb extension, it should be something like this,
require 'rubygems'
require "jekyll-import"
JekyllImport::Importers::CSV.run({
"file" => "my_posts.csv"
})
Lets say file name sample.rb. Then run this file using the following command,
ruby sample.rb

Use IRB console by typing irb at command prompt

Related

Trying to run a rspec test file and it can't find it

I am trying to learn RSpec, and thus starting with a simple calculator program.
Confused on how to have the rspec find the file because it keeps saying that it can't load it.
This is the process thus far:
1
Calculator class is written and saved as calculator.rb file. Create lib folder and move the calculator.rb file to it.
2
In the initial directory (i.e., outside of lib folder), I rspec --init which creates .rspec file & rspec/spec_helper.rb file.
3
Then I create write calculator_spec.rb file and put it in the rspec folder. On top of the calculator_spec.rb file, I have this require 'calculator.rb'
4
I run this command rspec calculator_spec.rb to which I get this 'load': cannot load such file
How do I fix this so the rspec command can find the file?
Thank you
This command would be in the first line of spec/spec_helper.rb:
require File.join(File.dirname(__FILE__), '../lib/calculator.rb')
Not sure which version of RSpec you are using but it should create a spec directory, not rspec in addition to the default .rspec file.
#jim-van-fleet's answer should work just fine. However, the best practice is to mirror your code directory under the spec directory. So it should look something like this:
I would also add the following to the top of your calculator_spec.rb file
require 'spec_helper'
require File.expand_path('../../../lib/calculator', __FILE__)

I have created a gem using bundle, now how to display some text to the user using this gem ?

I have created a gem called "quotes" using bundle. Now I want to display some 5 lines of text to the person on command prompt (not on console) say using
rake quotes show
or
quotes show
(I am not sure it will work or even possible, but looking for something like this, I am fine with any way of displaying the quotes)
How can I do it ?
If I understand you, you want to create an executable for your gem.
Create a directory bin (actually this could be any name, but the default is bin), and put a file quotes in it which is the Ruby script that you want to run from the command line, e.g.
#!/usr/bin/env ruby
puts "Whatever"
# other Ruby code as required ...
Now add an entry for executables in your gemspec:
spec.executables = ['quotes']
Now after you rebuild and install your gem, the quotes command will be available from the command line and will execute the script in thequotes file.
You should just be able to puts the output you want

.bashrc equivalent for rails console?

When I'm using the rails console I like having a clear! command along side the reload! command, so every time I launch the rails console I write
def clear!
system('clear')
end
When I repeat behavior in my bash shell I add it to my ~/.bashrc file. Is there a similar way for me to do this for my rails console?
Create a file in your home directory named ~/.irbrc. Inside, define any functions or settings you want to be applied to your irb.
Here's an example that explains what I mean.
You can do this with Pry if you use that instead of irb. You can configure custom commands in a ~/.pryrc
Pry.config.commands.command "clear!", "Clears the display" do |*args|
system("clear")
end
See pry-rails

What is the difference in running a rails unit test with -Ilib and -Itest?

I use both the following commands to run tests in my application:
ruby -Ilib test/unit/account_test.rb
ruby -Itest test/unit/account_test.rb
Both works fine. But, what is the difference. What actually the flag -I means?
From the Ruby manual page (man ruby):
-I directory Used to tell Ruby where to load the library scripts. Direcā€
tory path will be added to the load-path variable ($:).
The variable $: (aliased as $LOAD_PATH) is the array of directories where Ruby looks for files to load when you call require. In your case both commands works well because, I guess, you don't require anything inside lib or test directories from your test code.

How to run a .rb file from IRB?

I am starting out with Ruby on Rails. I am currently going through a tutorial where it says that I have to run a .rb file from IRB and that that will create a .xml file in my current directory.
My question is how do I run a .rb file in IRB?
And do I have to be in the directory where this .rb file lives when I run it in IRB?
I tried the following: just typing irb on the command line in the directory of the file. That starts an IRB session as far as I understand.
Then I typed irb "filename.rb" which went through but didn't create anything in the current directory but at least it didn't give any errors.
I also tried a whole bunch of other stuff that plain gave me errors. So I don't think I can solve this myself and googling the matter didn't help at all.
I am running Leopard.
You can "run" a file in irb by just requiring or loading it.
$ irb
>> load './filename.rb'
To change your current working directory within irb, you can use FileUtils:
>> require 'fileutils'
>> FileUtils.pwd # prints working directory
>> FileUtils.cd '/path/to/somewhere' # changes the directory
In case you want to have your file loaded in the irb session and you are using Ruby 2+ you can load a file in irb like this:
irb -r ./the_name_of_your_file.rb
This opens an irb session with the given file loaded.
Imagine you have file with a class like this:
class Example
def initialize(name)
#name = name
end
def print__example
p name
end
end
You will be able to use the Example class in the irb session.
We can just create a .rb file in the directory which you are currently working in using any text editor and type all the code in that and then use the command ruby filename.rb in the terminal, not in the irb, then it shows the output in irb.

Resources