Ruby require_relative executing script? - ruby

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.

Related

'require' not loading ANY file - strange error message - ruby

Expected result: call on another file in the same dir with information
that will apply to current file
FILE TO BE CALLED ON:
(Location: C:\require\class.rb)
class Member
##count = 0
def initialize
##count =+ 1
end
def self.count
##count
end
end
Bob = Member.new
puts Member.count
OUTPUT: A new member is made, the total member count is +1
FILE DOING THE CALLING:
(Location: C:\require\require.rb)
require "./class.rb"
Henry = Member.new
puts Member.count
RESULT: No new member is made, the class is not called, apparently Ruby is looking in different directories (I suppose) even though both filesare right there next to each other in the directory C:\require
(I've set up this whole thing to simplify it, so I can show stack overflow the problem without any unnecessary fluff)
C:/Ruby24-x64/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in
`require': cannot load such file -- ./class.rb (LoadError)
from C:/Ruby24-x64/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in
`require'
from C:/require/require.rb:1:in `<main>'
I'm only about 150 hours a developer. I apologize if this is a dumb question but I've exhausted my resources. It seems to work for everybody in every tutorial I watch flawlessly, so I'm quite dumbfounded. Thanks.
P.S. I'm using the Atom text editor (the output comes from pressing Alt + R, for those familiar)

Parsing file names using ruby Net::SSH

Working on automating a task for my job.
Issue: We have a rather large stack of git repos hosted on a private git server at our main office. I work at a remote location.
Goal: I need to clone many (50+) repos without authenticating and manually typing out the filename ever time.
This is supposed to be the quick and dirty version written in ruby (I'm currently learning ruby so I'm trying to use it for any and all needs it's good for) which I can use as a template for more fine tuned git operations in the future.
This is what I have right now:
#!/usr/bin/ruby
require 'net/ssh'
git_server_address = "x.x.x.x"
git_server_login = "username"
puts "Enter password for #{git_server_login}"
git_server_passwd = gets.chomp
$gitnames = []
def getGitNames(address, login, passwd)
Net::SSH.start(address, login, password: passwd) do |ssh|
ssh.exec!("cd /git")
ssh.exec!("find . -type d -exec echo '{}' \\;") do |file|
$gitnames.push(file)
end
end
end
$gitnames = getGitNames(git_server_address, git_server_login, git_server_passwd)
$gitnames.each do |file|
if file.equal?('slamros.git')
puts(file)
#puts system("git clone #{git_server_login}##{git_server_address}:/git/# {file}")
#puts git_server_passwd
end
end
If this is abhorrently ugly, I apologize, I'm still very new at my job and to professional programming. I know there are some best practices I'm not following but my goal was just to get this operational by EoD.
Essentially what I'm trying to do is use the ssh.exec! and find -exec to get the names of the files in the /git folder.
Once this is working I will use the filenames I have to pull git clones on my own system. I currently have these lines commented out.
My code currently asks for my password, processes for about 5 seconds, and then gives me this error:
/GitClone.rb:21:in `<top (required)>': undefined method `each' for nil:NilClass (NoMethodError)
from -e:1:in `load'
from -e:1:in `<main>'
I get that what i'm doing in my getGitNames() method is wrong, but I would appreciate some help in pointing out what exactly it is or some ideas of other ways to accomplish my task with Ruby.

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.

Ruby Net::SFTP does not work, even on its own example code

Trying to use Net::SFTP, version 2.05 (appears to be the latest). But it fails even on its own sample code (below, from http://net-ssh.rubyforge.org/sftp/v2/api/index.html). The domain, user and password are the same as I use to log in manually. I will change the paths when I can get that far.
Net::SFTP.start('ftp.domain.com', 'user', :password => 'password') do |sftp|
# upload a file or directory to the remote host
sftp.upload!("/path/to/local", "/path/to/remote")
# download a file or directory from the remote host
sftp.download!("/path/to/remote", "/path/to/local")
# grab data off the remote host directly to a buffer
data = sftp.download!("/path/to/remote")
# open and write to a pseudo-IO for a remote file
sftp.file.open("/path/to/remote", "w") do |f|
f.puts "Hello, world!\n"
end
# open and read from a pseudo-IO for a remote file
sftp.file.open("/path/to/remote", "r") do |f|
puts f.gets
end
# create a directory
sftp.mkdir! "/path/to/directory"
# list the entries in a directory
sftp.dir.foreach("/html") do |entry|
puts entry.longname
end
end
It hangs on the first line, and the traceback is below (line 14 is the Net::SFTP.start line)...
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:801:in `block in loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:212:in `preprocess'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:197:in `process'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:161:in `block in loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:161:in `loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-ssh-2.0.23/lib/net/ssh/connection/session.rb:161:in `loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:802:in `loop'
c:/ruby192/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp.rb:35:in `start'
C:/Data/dev/Ruby/RubyProjects/FTP_test/lib/ftp_test.rb:14:in `<top (required)>'
Windows 7, netbeans 6.9.1, ruby 1.9.2. The sample code is from the readme at..
When i used Net::SFTP in Perl i faced similar issue and its becos ssh path was not set properly.
Can you check whether SSH is in path.
I know the answer is not related to Ruby but suggested this as a general observation.

Telling rspec to not load files

I'm trying to add some commit hooks to my git repo. I want to leverage Rspec and create commit message specs that will run each time I commit. I have figured out how to run rspec outside of the 'spec' command, but I now have an interesting problem.
Here is my current code:
.git/hooks/commit-msg
#!/usr/bin/env ruby
require 'rubygems'
require 'spec/autorun'
message = File.read(ARGV[0])
describe "failing" do
it "should fail" do
true.should == false
end
end
This is throwing an error when it gets to the describe call. Basically, it thinks that the commit message it receives is the file to load and run the specs against. Here is the actually error
./.git/COMMIT_EDITMSG:1: undefined local variable or method `commit-message-here' for main:Object (NameError)
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:15:in `load'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:15:in `load_files'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:14:in `each'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/example_group_runner.rb:14:in `load_files'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner/options.rb:133:in `run_examples'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner.rb:61:in `run'
from /Users/roykolak/.gem/ruby/1.8/gems/rspec-1.3.0/lib/spec/runner.rb:45:in `autorun'
from .git/hooks/commit-msg:12
I am looking for a way to tell rspec to not load files. I have a suspicion that I will need to create my own spec runner. I came to this conclusion after viewing these lines in rspec-1.3.0/lib/spec/runner/example_group_runner.rb
def load_files(files)
$KCODE = 'u' if RUBY_VERSION.to_f < 1.9
# It's important that loading files (or choosing not to) stays the
# responsibility of the ExampleGroupRunner. Some implementations (like)
# the one using DRb may choose *not* to load files, but instead tell
# someone else to do it over the wire.
files.each do |file|
load file
end
end
But, I would like some feedback before I do that. Any thoughts?
Do you even really need all the special stuff that RSpec provides (should and the various matchers) just to verify the contents of a single file? It really seems like overkill for the problem.
spec/autorun eventually calls Spec::Runner.autorun which parses ARGV as if it held normal arguments for a spec command line.
When you install a bare “spec” file as a Git hook,
it will get arguments that are appropriate for the whatever Git hook is being used,
not spec-style arguments (spec filenames/directories/patterns and spec options).
You might be able to hack around the problem like this:
# Save original ARGV, replace its elements with spec arguments
orig_argv = ARGV.dup
%w(--format nested).inject(ARGV.clear, :<<)
require 'rubygems'
require 'spec/autorun'
# rest of your code/spec
# NOTE: to refer to the Git hook arguments use orig_argv instead of ARGV

Resources