I have an extremely simple hello.rb file containing only:
print 'Hello world!'
I then try to run this file from my Ubuntu 14 terminal using:
ruby hello.rb
However, this ends up looking just about like this:
user#machine:~/Documents/Ruby/HelloWorld$ ruby hello.rb
Hello world!user#machine:~/Documents/Ruby/HelloWorld$
I guess that's technically correct, but it would be more readable if a newline is inserted after the output Ruby's execution. For regular terminal commands such as dir this newline is inserted, and the prompt starts on a new line.In other words, I'd like to see this:
user#machine:~/Documents/Ruby/HelloWorld$ ruby hello.rb
Hello world!
user#machine:~/Documents/Ruby/HelloWorld$
What do I need to change to get this behavior? Do I need to change the way I call Ruby? Or should I change my terminal settings?
Use puts instead of print. It adds the newline.
Related
I'm writing a ruby script and found this strange behaviour.
Using ruby 2.4.2 [x86_64-darwin16]
Basically I'm trying to echo two separated messages and in my index.rb file I got:
exec("echo 'teste'")
exec("echo 'teste2'")
But when I run ruby ./index.rb
The output is:
teste
Why that's happening?
Shouldn't this be the output?
testeteste2
exec([env,] command... [,options])
Replaces the current process by running the given external command docs
It means that the first call to exec replaces your ruby program with echo, so the rest of the ruby program is not executed.
You can use backticks to run a command like you want:
`echo 'teste'`
`echo 'teste2'`
When running a ruby script with command line arguments, the "gets" is no longer blocking, it doesn't work.
test.rb
#!/usr/bin/ruby
puts "should wait for input"
gets
puts "test"
and here is how I run it
$ ./test.rb test.rb
should wait for input
test
It didn't wait.
I'm running Ubuntu 16.04 desktop, and Ruby from repository ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]
What am I doing wrong?
In addition to STDIN.gets like others have recommended, you can use plain gets if you call ARGV.clear beforehand. The regular gets works as expected if there aren't command like arguments to the script, but if there are, then it will read them. It's not really clear why you're using ./test.rb test.rb, but the second filename is a command line argument.
More specifically, if regular gets is called when ARGV is populated, then the result will be the contents of the file.
max#max ~> echo "content" > test.txt
max#max ~> ruby -e "puts ARGV.inspect; puts gets" test.txt
["test.txt"]
content
Nevermind,
The "gets" actually takes the first line from the file I added in the cli arguments.
Very weird.
I am trying to run my first Ruby program. I am using sublime. My programs code is as follows..
$ vim helloworld.rb
#!/usr/bin/ruby
# Hello world ruby program
puts "Hello World!";
I am getting the following output error...
C:/Users/myname/AppData/Roaming/Sublime Text
2/Packages/User/helloworld.rxml:1: `$ ' is not allowed as a global
variable name [Finished in 0.1s with exit code 1]
Can anyone help me, and google search did not turn up much.
Remove $ vim helloworld.rb from the file.
It's a common practice in the books to prefix the commands written in terminal with $, so if you see $ some_cmd, it's most probably written in terminal.
I wrote a Ruby script like the following example. The basic functionality is the same:
# get input from the user
input = gets.chomp
# do awesome stuf with this input and print the response
puts do_awesome_stuff(input)
The problem is when I run the script it prints the solution I want, but the console window closes right after. I want the console to keep open.
I'm currently on windows, but the solution should be working on every system.
One way is to run the ruby script with a .bat file and pause it, like so:
ruby script.rb
PAUSE
I hope there is a way without the additional .bat file. Does Ruby has a function like PASUE integrated?
It seems like you double click the ruby script file.
Instead issue the following command in cmd shell.
ruby filename.rb
If you don't want that, you can add gets to the end of the script.
# get input from the user
input = gets.chomp
# do awesome stuf with this input and print the response
puts do_awesome_stuff(input)
gets # <----
But this is not recommended because .. if you run the command in cmd shell or terminal you should type extra Enter to return to the shell.
Use the -r options of irb.
irb -r ./filename.rb
I was trying to figure out how to work the command line switch -r.
My understanding is that the code is typed out as follows:
ruby -r*nameOfRequired*
I am finding that this is not the case. When I type out the above and press enter, the terminal expects an "end of input syntax" and does not continue.
What am I missing? Does there need to be a space in between the switch and the name of the required file?
Please and thank you!
EDIT:
I am currently reading "The Well Grounded Rubyist" by David A. Black, and I came up with this question while reading the section on command line switches.
Having said that, I created a "test.rb" file, containing:
puts Date.today
Then, in the terminal, I typed out:
ruby -r date
I thought this would 'require' the date module, and then enable me to run the "test.rb" file, using ruby test.rb (given that I am in the correct directory).
Instead, the terminal cursor moves to a newline, expecting more input. Let me know if I need to clarify anything else. Thanks!
If you just type ruby -rmodule, then Ruby will load the module and wait for you to type the main program that requires that module.
If you just want to run the module and do nothing else, you can do do rubyfull-path-to-module without the -r, or ruby -rmodule -e exit, or ruby -rmodule </dev/null, or similar.
In general, the ruby command does not record any state from one run to the next, so you need to tell it every thing that it needs to know whenever you run it.
Whenever you run it, you need to tell it the main program to run or else it will expect you to type that program on the standard input. The -r does not specify the main program.
Try this:
ruby -rdate test.rb
According to ruby -h:
-rlibrary require the library, before executing your script
Without giving your script file path, it read the script from stdin.
Try following (You can omit script file path when you give -e command):
ruby -r**nameOfRequired** -e ""