ruby and erb not responding in centos - ruby

I've installed ruby with rvm on a centos 6.3 VM and when I attempt to run ruby with no parameters (or erb for that matter) I get no response back, it just sits there.
Running 'ruby' just drops the cursor to the next line like it's waiting for more input or something else to happen. Typing any ruby commands during this time does nothing AFAIK. I do not get any errors, but I'm not exactly sure where to look. I have to control-c to break out of it.
Few interesting things:
(I'm fairly new to linux, so be gentle)
running the command ruby -v
gets me:
ruby 1.9.3p374 (2013-01-15 revision 38858) [x86_64-linux]
so it seems like it's installed
running:
echo "puts 'hello'" |ruby
gets me:
hello
I've created a different user and then logged in with that user and the behavior is the same.
I've installed rails and then created a rails app and I can do rails c use the rails console
Other notes that may or may not be relevant:
I've recently installed KDE. I did have ruby working before installing KDE but I can't say it was the installation of KDE that messed it up.
I've already uninstalled ruby using rvm and reinstalled, same thing.
Uninstalled rvm and reinstalled.
I've no idea where to go with this or what information would be useful.

Run irb to get an interactive session. This is a REPL, a Read-Eval-Print Loop.
To see Ruby do something in response to running the interpreter and feeding it standard input interactively, try something like:
$ ruby
p :hello
^D
If you just type ruby, I'm not sure anything will happen until it reads the entire input file ... i.e., sees a Control/D indicating standard input end-of-file.

I believe your looking for irb - the interactive Ruby interpreter.
erb is an interpreter that expects a file argument.
$ erb hello-world.rb

Related

How to run app via Ruby shell command, when same command works on command line

I have a simple Ruby (Sinatra) server that starts up without issue from the command line with ruby app.rb. But when I execute the same command via my command line app, either with `ruby app.rb` or with system("ruby app.rb"), I get this error:
app.rb:1:in `require': cannot load such file -- sinatra (LoadError)
from app.rb:1:in `<main>'
The opening line of app.rb is simply require 'sinatra'. The Sinatra gem is installed in my system, of course; I don't understand why the interpreter is acting as if it's not there.
While troubleshooting, I decided to add Sinatra to the Gemfile of the command line app that is calling app.rb. Lo and behold, now that the parent process has access to Sinatra, now it works (i.e., system(app.rb) successfully starts the Sinatra server). But when I exit the command line app, a Sinatra server is always there, saying:
[2018-12-18 23:17:37] INFO WEBrick 1.3.1
[2018-12-18 23:17:37] INFO ruby 2.4.0 (2016-12-24) [x86_64-linux]
== Sinatra (v2.0.4) has taken the stage on 4567 for development with backup from WEBrick
[2018-12-18 23:17:37] INFO WEBrick::HTTPServer#start: pid=27384 port=4567
So I have to Ctrl-c to exit the command line app.
Question: Is there a way to spawn an independent Sinatra process/server, as I was trying to do with system("ruby app.rb"), without installing it in the parent app (the command line app)? I also tried using Process.fork followed by Process.wait, but that didn't help.
Self-answered, since I found a solution, and nobody else has answered it:
Instead of executing just ruby app.rb, which by itself runs in the same environment as the surrounding program and hence uses the same Gemfile (ignoring the one referred to in the spawned script), execute:
system("BUNDLE_GEMFILE='./Gemfile' && ruby app.rb")
That's all there was to it! This tells Ruby to load the correct gemfile when it runs the program.
Personal note: Many thanks to all of you in the discussion, above, on the question, as well as the local Ruby group, my best friend, and my 12-year-old coder son (who actually supplied the exact code). Now I just have to figure out how to stop the spawned process without stopping the surrounding program. Can't just use "Ctrl-C." But that's a different problem!

Is /etc/irbrc installed by OS X? Does irb read it?

While investigating an issue with irb on my Mac (OS X 10.11.5) I noticed /etc/irbrc. The first few lines follow:
# Some default enhancements/settings for IRB, based on
# http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks
unless defined? ETC_IRBRC_LOADED
# Require RubyGems by default.
require 'rubygems'
# Activate auto-completion.
require 'irb/completion'
# continued ...
It seems to be out of date (rubygarden.org is gone, rubygems is in the standard library these days) and does things that I always thought I had to do in my own ~/.irbrc (set up completion, history, etc.).
It is dated 31 Jan 2016. I might or might not have run an Apple system update or upgraded something else on that date; I don't recall. I've definitely upgraded OS X by a major version or two since then.
I don't think I have any Rubies installed on this computer other than that from OS X and a Ruby 2.3.1 installed with rbenv (in my own account, not as root). That is, I don't think this file could have come from anything other than OS X.
Unix shell convention would lead me to expect that a file named /etc/irbrc would be executed when any user ran irb, before their ~/.irbrc if they had one. However, the irb installed by OS X doesn't appear to read this file: I put puts 1 at the top and don't see the result when I run irb. (I normally use rbenv, but disabled it while investigating this file.) /etc/irbrc doesn't appear to run whether or not I have an ~/.irbrc. Also, I see no mentions of this file in /usr/bin/irb or /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/irb.
My guess is that this file
is installed by OS X
is not read by irb as is (despite its misleading name and location), but is intended to be copied to ~/.irbrc.
Does anyone know for sure, or know any different?
I don't need or want it; I'm happy to maintain my own ~/.irbrc. I just want to be sure that it isn't affecting irb when I run it (in particular, the irb in the rbenv-installed Ruby that I normally use), and that future OS X upgrades won't change irb behavior.
Per Mark Setchell's and Jared Beck's comments, /etc/irbrc is installed with current (10.13) OS X and has been for at least a few major releases.
I copied /etc/irbrc to ~/.irbrc, ran irb, exited and got an error: undefined method 'nitems' for ["exit"]:Array (NoMethodError). This method existed in Ruby 1.8 but was removed from Ruby 1.9. I don't normally see this error, so I conclude that /etc/irbrc isn't executed at all.
Overall, I conclude that I can ignore /etc/irbrc when debugging issues with my ~/.irbrc, which was the origin of my question.

Executing a .rb file (Ruby file) with PRY in Windows 7

I was wondering how I can load a Ruby file into PRY to use the debugger function on my Windows 7 computer with Command Prompt. I want to use PRY to go through my program step by step and following the tutorials online on how to do it aren't working for me (probably because all of them are using Mac Terminal)
I use SublimeText 2 to write everything up and then save it as a .rb file and I was told that if I wanted to debug the file I just made, I need to run it through IRB or PRY. Not, I already ran "gem install debugger". And the Ruby I have installed is 1.9.3 - p545. Do I need to add an extra line of code that says "binding.pry" (I saw that in some of the samples that I could find on stackoverflow). All I'm looking for is a simple step-by-step process. Thank you for reading this, I look forward to the responses.
Make sure you've installed pry and pry-debugger gems
At the top of your file, add statements for require 'pry' and require 'pry-debugger'
In your code, wherever you want to start ddebugging, just add a statement binding.pry
Now, you can run your file like ruby filename.rb and the debugger should open

Validating ruby installation after install?

I've installed Ruby 2.1.1 via source.
I've seen suggestions to type ruby -v, which I assume would show that the binary isn't corrupted, but are there more comprehensive ways to ensure that it's working as expect? Unit-tests, benchmarks, etc to validate it's functional?
Run make test after compiling in the directory you’ve compiled in. (This might actually happen by default, I can’t remember. There’s also make test-all, among others.)
ruby -v will show you the current version of ruby installed on your machine.
If you want, just create a hello.rb with puts "hello" and run it using ruby hello.rb to check if it is interpreting the ruby code correctly. So you know that its functional.

Is there a trick to profiling memory usage w/ ruby-prof using an rvm gemset?

I've been trying to get ruby-prof working for memory usage on Mac OS X w/ Ruby 1.8.7 and so far I'm not having much luck.
I understand from the documentation that RubyProf::MEMORY mode requires a patched version of Ruby. I've tried installing the GC-patched version (which I found referenced in the Rails documentation) through RVM:
rvm reinstall 1.8.7 --patch ruby187gc
Based on the output of this command, everything looks good. I clearly see "Applying patch 'ruby187gc'" in the console output.
The problem seems to come when trying to use an RVM gemset. I wrote the following script to test this:
require "ruby-prof"
RubyProf.measure_mode = RubyProf::MEMORY
results = RubyProf.profile do
# code to measure
end
File.open(File.join(File.dirname(__FILE__), "profile-graph.html"), "w") do |file|
RubyProf::GraphHtmlPrinter.new(results).print(file)
end
When I preceed this script with rvm use 1.8.7 --patch ruby187gc, it creates the file "profile-graph.html" showing memory usage of all the different method calls in the script, as I'd expect. However, when I try something like rvm use 1.8.7#gemset_name --patch ruby187gc, the output file contains all "nan" values.
What gives? Is there some special trick to using a patched version of Ruby along with an RVM gemset? Does the problem lie elsewhere? I'm crossing my fingers and hoping that someone has run into a very similar problem and figured this out before.
Ah, I was overcomplicating things. Turns out I just needed to uninstall and reinstall the ruby-prof gem (after patching Ruby).

Resources