Problems running rake with rspec and ruby - ruby

I am trying to run rake on a ruby file where I am supposed to receive an error so I can debug it as an exercise. However the error I get is not the error Im supposed to receive. I'm receiving the following and I'm having a time interpreting what I need to fix.
~Desktop/learn_ruby-master/00_hello$ rake
(in /~Desktop/learn_ruby-master)
/usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- hello (LoadError)
from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from ~Desktop/learn_ruby-master/00_hello/hello_spec.rb:117:in `<top (required)>'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
rake aborted!
/usr/bin/ruby1.9.1 -S rspec ~Desktop/learn_ruby-master/00_hello/hello_spec.rb - I/~Desktop/learn_ruby-master/00_hello -I/~Desktop/learn_ruby-master/00_hello/solution -f documentation -r ./rspec_config failed
Tasks: TOP => default => spec
( See full trace by running task with --trace)
Here is the code I am running rake on
require "hello"
describe "the hello function" do
it "says hello" do
hello.should == "Hello!"
end
end
describe "the greet function" do
it "says hello to someone" do
greet("Alice").should == "Hello, Alice!"
end
it "says hello to someone else" do
greet("Bob").should == "Hello, Bob!"
end
end

I had the same problem (going through the exact same tutorial). I just spent three days of my life trying to figure it out. The issue was that one of my folders in the path to 'hello.rb' had a space in between two words. Seriously, that was it. The ruby path couldn't pick it up no matter what I did (except change the space). Uggh. Lesson learned, don't name anything, anywhere with a space from here on out.

Ruby says it all: "Cannot find filename hello.rb on the library loading path". You are missing hello.rb file or Ruby cannot find it. Do you really have it on the disc in the directory you run rake from? If its somewhere else you need to provide relative path.
Also remove the whitespace from the first line, I suspect you have some gargabe there. There should be only one space between -- and hello in the error message.

Related

Rubymine file reference

I'm trying to reference a file to write to it using RubyMine and I'm having trouble figuring it out. When using the full path, the code errors out while running it with RubyMine.
When I use the same code and run it in terminal, the code works fine using the command:
ruby studio_game players.csv
How can I get the file to be recognized without having to designate the full path in RubyMine?
Erring Code:
require_relative 'player'
require_relative 'game'
player1 = Player.new("moe")
player2 = Player.new("larry", 60)
player3 = Player.new("curly", 125)
knuckleheads = Game.new("Knuckleheads")
knuckleheads.load_players(ARGV.shift || 'players.csv')
Error message:
/Users/MNickey/.rvm/rubies/ruby-1.9.3-p448/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) "/Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/studio_game"
/Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/game.rb:83:in `readlines': No such file or directory - players.csv (Errno::ENOENT)
from /Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/game.rb:83:in `load_players'
from /Users/MNickey/RubymineProjects/PragmaticStudio/Stooges Game/studio_game:9:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
knuckleheads.load_players(ARGV.shift || 'players.csv') is almost certainly what is causing the problem. Unless you have configured the launcher for the script with a command line argument, ARGV will be empty and you will be getting plain-old 'players.csv' as the result.
I'm pretty certain you are running the script with an argument when you run it in the command line.
To set a command line parameter in RubyMine, go to Run / Edit Configurations, find the launch configuration for your script, and add the path to your csv file to the Script Arguments input box.

How to make a dir in Ruby

So I'm making this project in Ruby, and I copied this code from somewhere and It's not working.
Code:
dirname = File.dirname("C:/ProgramFiles/RubyLists")
require 'fileutils'
unless File.directory?(dirname)
File.mkdir(dirname)
end #This block will make the directory.
print("Mk. Worked.")
Error:
C:/Users/User/RubymineProjects/rubylists/main.rb:6:in `<top (required)>': undefined method `makedir' for File:Class (NoMethodError)
from -e:1:in `load'
from -e:1:in `<main>'
If you need anymore info let me know and I will provide it if I can. Thanks!
FileUtils::mkdir exist, not File::mkdir.
Thus change File.mkdir(dirname) to FileUtils.mkdir(dirname).
Write your code :-
dirname = "C:/ProgramFiles/RubyLists"
require 'fileutils'
unless Dir.exist?(dirname)
FileUtils.mkdir(dirname)
end #This block will make the directory.
print("Mk. Worked.")
Since you're using FileUtils you can use mkdir
FileUtils.mkdir("a/b/c")
though if any of the parent folders don't exist it would just crash. I typically use mkdir_p since it recursively creates directories as needed (unless I want it to crash, for example if the folder names were wrong)
# function for create folder
def createFolder(folderName)
#folderName=folderName
if File.directory?(#folderName)
return "The Folder "+#folderName+" already exist"
else
Dir.mkdir(#folderName,0700)
return "Created"
end
end
to call it just type
createFolder('folderName')

How do I reduce the output in a stack trace?

Is there any way to reduce the amount of output provided by Ruby when there's an error?
For example:
rspec bowling_spec.rb
/Users/snowcrash/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- bowling (LoadError)
from /Users/snowcrash/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from /Users/snowcrash/Developer/Code/Ruby/RSpec/bowling_spec.rb:2:in `<top (required)>'
from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `load'
from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `each'
from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/command_line.rb:22:in `run'
from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/runner.rb:80:in `run'
from /Users/snowcrash/.rvm/gems/ruby-2.0.0-p195/gems/rspec-core-2.14.2/lib/rspec/core/runner.rb:17:in `block in autorun'
All I'm interested in is the first line, cannot load such file -- bowling (LoadError). Ideally I'd like Ruby to not spit out the rest of the from lines.
Is this possible?
Do something like this:
module Kernel
at_exit do
case $!
when nil, SystemExit, Interrupt
else puts $!.message, $#.first
end
$stderr.reopen(IO::NULL)
$stdout.reopen(IO::NULL)
end
end
If you're on a Unix/Linux OS, and you're running tests from the command line, you can do
rspec bowling_spec.rb | head -n 20
to ensure you don't have to scroll up to see your error.

Ruby require_relative executing script?

I'm working on a game server in ruby, and during testing I'm having trouble testing components individually. I wasn't getting output from my launcher, just the server, so I commented out the initialisation of the server- yet eclipse still showed output from the server!
I then went to the command line, assuming eclipse was looking at the wrong file (git has messed it around before, but as you can see, the stack trace shows that Server.rb is being executed in its entirety from line 5: require_relative 'Server':
This is the text content of the file:
class Launcher
puts "File saved at #{File.mtime($0)}"
require_relative 'Server'
require_relative 'Game'
#STDOUT.sync = true
puts "Launcher started"
#server = Server.new
print "server made"
game = Game.new
#serverThread = Thread.new{server.start()}
gameThread = Thread.new{game.start()}
while (running)
print "Stop? "
input = gets.chomp
if (input.equals?("yes"))
running = false
end
end
server.stop
game.stop
gameThread.join
serverThread.join
end
and the terminal output:
C:\Users\gossfunkel\git\citadelserver\RubyCitadelServer>ruby Launcher.rb
File saved at 2013-06-22 18:16:44 +0100
Server starting up at 2013-06-22 18:16:47 +0100...
C:/Users/gossfunkel/git/citadelserver/RubyCitadelServer/Server.rb:20:in `recvfro
m': Interrupt
from C:/Users/gossfunkel/git/citadelserver/RubyCitadelServer/Server.rb:2
0:in `run'
from C:/Users/gossfunkel/git/citadelserver/RubyCitadelServer/Server.rb:1
5:in `start'
from C:/Users/gossfunkel/git/citadelserver/RubyCitadelServer/Server.rb:3
0:in `<class:Server>'
from C:/Users/gossfunkel/git/citadelserver/RubyCitadelServer/Server.rb:1
:in `<top (required)>'
from Launcher.rb:5:in `require_relative'
from Launcher.rb:5:in `<class:Launcher>'
from Launcher.rb:1:in `<main>'
How do I require a file without this happening, and should it be?
I can't tell without seeing the classes, but I would guess that either
game.start is doing more than you thought, and is starting the server for itself
You are, as your subject line suggests, running a different file from the one you are editing (or not saving the file once it is changed). Check by putting an obvious puts at the top of the program. Something like
puts "File saved at #{File.mtime($0)}"
should do the trick
After discussion, it seems there's a third option. The code in Server.pm creates and runs a server as well as defining the class. You need to remove the require as well as the lines that use the Server class.

Running my first RSpec test, is there any way to print just the error with no backtrace?

I'm looking at this tutorial online:
https://www.relishapp.com/rspec/docs/gettingstarted
# game_spec.rb
describe Game do
describe "#score" do
it "returns 0 for all gutter game" do
game = Game.new
20.times { game.roll(0) }
game.score.should == 0
end
end
end
Running the first example, I noticed that the error message they list is much shorter than what I'm seeing. On my terminal the stack trace is so long it scrolls off the top. Is there any way to shorter error message / stack output to make this fit?
/testspec/game_spec.rb:3:in `<top (required)>': uninitialized constant Game (NameError)
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `block in load_spec_files'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `each'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/configuration.rb:789:in `load_spec_files'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:22:in `run'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'
from /.rvm/gems/ruby-1.9.3-p194/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:17:in `block in autorun'
I stripped my home dir from the list of dirs there. Is it possible to order verbosity down to simply the NameError line?
As I guessed in the comment, there is an error with your configuration/installation.
`require': cannot load such file -- ./game
Ruby can not find that file. Make sure you have the correct file in place, and run the test again.

Resources