Rubymine file reference - ruby

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.

Related

How to use `Dir` to reference directories outside the working directory

The closest issue I could find on Stack Overflow was "Path outside of project, Ruby Dir", but the user appears to have been in the wrong development environment.
My issue is that the Dir class only gives information relative to the working directory. For instance, if I'm in C:/Users/john/Developer and this directory only has two subdirectories A and B then
$ ruby -e "p Dir['*']"
will return ["A", "B"]. This seems intended, but what if there is a directory C:/Users/jane/Developer/C that I need to run a command in using a script in C:/Users/john/Developer?
Running
Dir.chdir('C:/Users/jane/Developer/C') { %x[lerna clean] }
returns the error:
-e:1:in `chdir': No such file or directory # dir_s_chdir - C:/Users/jane/Developer/C (Errno::ENOENT)
from -e:1:in `<main>'
Is there a way to access directories outside of the working directory using the Dir class? It seems absurd to have to require scripts to be written at a root level.
Note, using %x[] to run the command in C:/Users/jane/Developer/C also doesn't seem to work.

Require Not Working in Ruby

I have two files person.rb and contact_info.rb and the person.rb file contains a class, and the contact_info.rb file contains a module. When i do load 'person.rb' in irb this works fine when load 'contact_info.rb' is at the top of this file.
When I switch this to require 'contact_info.rb' at the top of the person.rb file, and in irb do require 'person.rb' I get an error (i've included the error at the bottom of this text and it's using a completely different file path.
I've googled some solutions such as using './person.rb' and require_relative 'person' but these don't work either.
I've simplified the code within files to make things easier.
Any help would be awesome.
CODE IN THE person.rb FILE
require 'contact_info.rb'
class Person
include ContactInfo
end
CODE IN THE contact_info.rb FILE
module ContactInfo
#some code
end
ERROR MESSAGE THAT I'M GETTING - when I type in require 'person.rb' in irb.
**note - when i drag the file into the command line the file path is /Volumes/New\ Passport/All\ Creative/Ruby/module_folder_tut/person.rb
LoadError: cannot load such file -- person.rb
from /Users/paulknight/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/paulknight/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from (irb):1
from /Users/paulknight/.rvm/rubies/ruby-2.4.1/bin/irb:11:in `<main>'
Current directory in NOT on the ruby search path by default. Add it to $: and everything will be fine (assuming you are launching irb from the directory where person.rb is located):
$: << "."
require "person.rb"
More detailed info.

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.

Problems running rake with rspec and 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.

Ruby load/require files

What I'm attempting to do in Ruby is the equivalent to PHP's include or require statement.
In PHP, when you include or require a file, any PHP code in the included file is executed and globally scoped variables can be used in the file that houses the include.
In Ruby, I have this code;
# include file snippet
config = {'base_url' => 'http://devtest.com/'}
# main file
Dir.foreach(BASE_ROOT_PATH + 'config') do |file|
if !(file == '.' || file == '..')
filepath = BASE_ROOT_PATH + 'config/' + file
load filepath
end
end
config.inspect
The problem I'm encountering is that when I run the main file, it always errors out with this error;
/home/skittles/devtest/bootstrap.rb:24:in `<top (required)>': undefined local variable or method `config' for main:Object (NameError)
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from index.rb:27:in `<main>'
I know that it's pulling in the file because it's not throwing any errors.
I tried load & require, both with the same error. I tried config.inspect and puts config, still same error.
It's almost like the included file is not executing the code inside it. Is that what's going on or am I simply doing something wrong?
Thanks
config isn't a globally scoped variable - it's a local variable and local variable scope does not stretch across a require or load.
If you did want a global variable then you need to call it $config - the prefix denotes whether a variable is local (no prefix), global ($) or an instance variable (#). A constant might also be appropriate.

Resources