How do I fix this error when creating a ruby gem - ruby

I'm following a tutorial to create ruby gems http://guides.rubygems.org/make-your-own-gem/
The tutorial tells me to create a ruby file like this:
% cat lib/hola.rb
class Hola
def self.hi
puts "Hello world!"
end
end
Then a gemspec file like this:
% cat hola.gemspec
Gem::Specification.new do |s|
s.name = 'hola'
s.version = '0.0.0'
s.date = '2010-04-28'
s.summary = "Hola!"
s.description = "A simple hello world gem"
s.authors = ["Nick Quaranto"]
s.email = 'nick#quaran.to'
s.files = ["lib/hola.rb"]
s.homepage =
'http://rubygems.org/gems/hola'
end
When I gem build hola.gemspec I get this error:
Invalid gemspec in [hola.gemspec]: hola.gemspec:1: syntax error, unexpected tIDENTIFIER, expecting $end
% cat hola.gemspec
^
ERROR: Error loading gemspec. Aborting.
Now his code on Github will not build without the Rakefile.
So how can I make this work? Do I need to add a Rakefile or is there something wrong with the code?

Your error indicates that your file has the line % cat hola.gemspec in it literally. This line in the example isn't intended to be part of the file itself; it's the Unix command the author used to print the contents of the file. Remove that line and the similar line from the other file and you should be OK to move to the next step.

The first line, % cat lib/hola.rb is not meant to be part of the file, but rather the whole thing is command-line output. cat is a command used to output the contents of a file, and things like % and $ are often used to denote the start of a command. So, remove the first line from the file.

Related

Aruba: Command "seedly-calculator" not found in PATH-variable

So, I am trying to run the test but I am getting an error says.
Aruba::LaunchError:Command "seedly-calculator.rb" not found in PATH-variable
-seedly-calculator
-bin
-src
-seedly-calculator.rb
I have tried to change the path in rake file but it doesn't work.
My seedly-calculator.rb file is in the root directory.
require "rspec/core/rake_task"
namespace :spec do
desc "Run the functional suite against the CLI"
RSpec::Core::RakeTask.new(:functional, [] => [:set_path])
task :set_path do
project_bin_dir = File.join(File.dirname(File.expand_path(__FILE__)), '..', 'bin')
ENV['PATH'] = project_bin_dir + ':'+ ENV['PATH']
end
end
it shows error like:
Failure/Error: let(:command) { run "seedly-calculator.rb" }
Aruba::LaunchError:
Command "seedly-calculator.rb" not found in PATH-variable "/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/bin:/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/exe:/Users/bilaltariq/.rbenv/versions/2.6.2/lib/ruby/gems/2.6.0/bin:/Users/bilaltariq/Desktop/seedly-calculator/functional_spec/../bin:/Users/bilaltariq/.rbenv/versions/2.6.2/bin:/usr/local/Cellar/rbenv/1.1.1/libexec:/Users/bilaltariq/.rbenv/shims:/Users/bilaltariq/.asdf/shims:/Users/bilaltariq/.asdf/bin:/usr/local/bin:/Users/bilaltariq/.bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin".
I expect it to hit the file so i can write some test.
am i doing something wrong?
require 'spec_helper'
RSpec.describe 'Command Validation', type: :aruba do
let(:command) { run "seedly-calculator.rb" }
it "wrong/missing arguments" do
command.write("lookup\n")
stop_all_commands
expect(command.output).to end_with("Missing bank_name argument.\n")
end
end
seedly-calculator.rb:
#!/usr/bin/env ruby
# Complete bin/setup so that after it is
# run, ruby seedly-calculator.rb can be used to launch
# it.
# frozen_string_literal: true
require_relative './src/runner'
if !ARGV.length.zero?
input = ARGV
Runner.new.send('process_input', input)
else
puts "Arguments required!."
end
Update
To run a ruby script using run you need to make sure your ruby script is executable and contains a shebang so your system knows to run it with ruby. Here's example from this starter example
#!/usr/bin/env ruby
file = ARGV[0]
if file.nil? || file.empty?
abort "aruba-test-cli [file]: Filename is missing"
elsif !File.exist? file
abort "aruba-test-cli [file]: File does not exist"
end
puts File.read(file).chomp
So in your case you'll need to add this to the first line of your seedly-calculator.rb file
#!/usr/bin/env ruby
Then run this from command line to make it executable.
chmod +x #!/usr/bin/env ruby
I made a simple example forked off the one I reffed above. See this commit
Rspec convention is that it should match the same file structure of your project. It is not a good idea to set PATH manually.
Rake tasks are normally put in a tasks folder so you should have in project root a tasks folder
my_project/tasks/something.rake
Then you should have a spec folder that matches
my_project/spec/tasks/something_spec.rb
Then you should be able to get rid of task :set_path do end block and just run the spec without that.
You should also have a Gemfile to load your gems, run bundle install then invoke your test with
bundle exec rspec spec/tasks/sometask_spec.rb

Ruby saying file doesnt exist

I'm new to Ruby, and I am writing a test program just to get some of the features down. Here is the program
#!/usr/bin/env ruby
class FileManager
def read_file(filename)
return nil unless File.exist?(filename)
File.read(filename)
end
end
if __FILE__ == $0
fm = FileManager.new
puts "What file would you like to open?"
fname = gets
puts fm.read_file fname
end
As you can see, it is very simple. If I comment the first line of the read_file method, I get this error
No such file or directory - /Users/macuser/Projects/Aptana\ Studio\ 3\ Workspace/Ruby\ Test/text (Errno::ENOENT)
from /Users/macuser/Projects/Aptana Studio 3 Workspace/Ruby Test/ruby.rb:6:in `read_file'
from /Users/macuser/Projects/Aptana Studio 3 Workspace/Ruby Test/ruby.rb:15:in `<main>'
when I run the program and use this file: /Users/macuser/Projects/Aptana\ Studio\ 3\ Workspace/Ruby\ Test/text
However, if I run cat /Users/macuser/Projects/Aptana\ Studio\ 3\ Workspace/Ruby\ Test/text, it outputs Hello, world!, as it should.
I don't believe it's a permissions issue because I own the folder, but just in case I've tried running the program as root. Also, I have made sure that fname is the actual name of the file, not nil. I've tried both escaped and unescaped versions of the path, along with just text or the full path. I know for a fact the file exists, so why is Ruby giving me this error?
With gets filename the filename includes a newline \n.
You have to remove it in your filename:
gets filename
p filename #"test.rb\n"
p File.exist?(filename) #false
p File.exist?(filename.chomp) #true
(And you don't need to mask the spaces)
It looks like you're shell-escaping your spaces even though gets does not go through a shell. You want to enter "/Users/macuser/Projects/Aptana Studio 3 Workspace/Ruby Test/text" instead of "/Users/macuser/Projects/Aptana\ Studio\ 3\ Workspace/Ruby\ Test/text".

Add a Ruby command-line application to /usr/bin?

I have written a shell program in Ruby. Now I want to add it to my bin directory so that I can call the program by running $ my-rb-prog ....
First I tried to symlink my file into /usr/bin but then it said that it couldn't load the modules that I required.
On my second try, I tried to build a gem out of my project which worked fine, but I can still not access my shell program. After that I installed the gem. Here's what my gemspec looks like:
# -*- encoding: utf-8 -*-
$:.unshift(File.join(File.dirname(__FILE__), "/lib"))
require 'webcheck'
Gem::Specification.new do |s|
s.name = "webcheck"
s.version = WebCheck::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Victor Jonsson"]
s.email = ["kontakt#victorjonsson.se"]
s.homepage = "http://victorjonsson.se"
s.summary = %q{Check your website man!}
s.description = %q{Just check it!}
s.required_ruby_version = '>= 1.9.3'
s.add_dependency "httparty", "~> 0.12.0"
s.post_install_message = "Just check it!"
s.files = `git ls-files`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
end
I thought that I would get access to my shell program, which is a Ruby file located in the bin directory inside my project, after that I had installed the gem but it clearly isn't that easy.
This is my first day of coding Ruby if you can't tell.
First, add a "she-bang" string as first line of your file. It will allow the shell to run the file. It should be:
#!/usr/bin/env ruby
Then give execution permissions to the file:
$ chmod +x your_file_name.rb
Now you can run your application:
./your_file_name.rb
Also you can add the path to the directory with this script to the PATH variable and run the application from anywhere you want.
# You may do this in ~/.bashrc file
PATH=$PATH:path/to/dir/with/script/
Don't forget to add #!/usr/bin/env ruby to the top of your Ruby script.
"Making a Ruby Script Executable" is a really good tutorial on making your executable available system wide, without the use of a gem.

Ruby-rspec - how to print out the test (group) name

If I have some test, e.g.
require_relative "Line"
require_relative "LineParser"
describe Line do
it "Can be created" do
load "spec_helper.rb"
#line.class.should == Line
end
it "Can be parsed" do
...
How can I print out the test group name - "Line" in this case.
I tried adding:
before :all do
puts "In #{self.class}"
end
but that gives: In RSpec::Core::ExampleGroup::Nested_3, not Line
You may have specific reasons for wanting access to the test name while you're in the test...however, just in case it fits your needs to just have the line output in the test report, I like this configuration:
RSpec.configure do |config|
# Use color in STDOUT
config.color_enabled = true
# Use color not only in STDOUT but also in pagers and files
config.tty = true
# Use the specified formatter
config.formatter = :documentation
end
This gives me output like:
MyClassName
The initialization process
should accept two optional arguments
RSpec will read command line arguments from a file, so you could add the following to a .rspec file in the root of your project:
--format documentation
--color
(This file may already exist depending on the gem you're using for RSpec and how you've installed it.)
The answer turned out to be:
before :all do
puts "In #{self.class.description}"
end
$ rspec line_spec.rb
In Line

Running Ruby scripts from command line

I have a 2 scripts:
test1.rb
require 'test2.rb'
puts "hello"
test2.rb
puts "test"
I'm running this by executing ruby test2.rb test1.rb.
But only test is printed out and not hello.
You only need to run ruby test1.rb and the require statement should pull in test2.rb for you - you don't need to put it on the command line as well. (That will try and run test2.rb, passing the string 'test1.rb' as an argument, which is not what you want here)
Edit: the require statement does not look in the current directory by default when trying to find 'test2.rb'. You can explicitly specify it by changing it to:
require File.dirname(__FILE__) + '/test2.rb'
in test1.rb do (assuming test2.rb is in same directory, otherwise give its path relative to test1.rb)
require_relative 'test2.rb'
puts "hello"
and on the command line just do ruby test1.rb
This should work as well
require './test2.rb'
puts "hello"
There are some explanation how you can solve your problem, but not what is going wrong.
With ruby test2.rb test1.rb you call the ruby script with the parameter test1.rb.
You have access to the parameters in the constant ARGV.
An example with this script:
puts "test"
puts 'ARGV= %s' % ARGV
The result when you call it:
C:\Temp>ruby test.rb test2.rb
test
ARGV= test2.rb
So you could also write a program like:
require_relative ARGV.first
The first parameter defines a script to be loaded.
Or if you want to load many scripts you could use:
ARGV.each{|script| require_relative script }

Resources