Ruby hello world in sublime - ruby

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.

Related

Ruby directly from command line

I know that I can run ruby code directly from the command line, like so:
ruby file.rb
But is there any way to run ruby code directly from the command line so that I don't have to save the file in the first place?
ruby -e 'puts("foobar :)"); puts(2 + 2)'
should print foobar :) and 4
The quick help invoked using ruby -h says:
$ ruby --help
Usage: ruby [switches] [--] [programfile] [arguments]
...
-e 'command' one line of script. Several -e's allowed. Omit [programfile]
...
#djaszczurowski's answer provides you an example.
Another, better, option is to use irb (the interactive Ruby interpreter). It displays a prompt and waits for you to enter Ruby code. Multiple lines of code can be entered; it is executed when the block is closed.
#ho-man's answer shows you how to use it.
If you have more code that you'll like to run you can use irb instead.
$ irb
2.4.1 :001 > puts (2+2)
4
=> nil

how to build wrapper script

Sort of an odd question, but: how would one go about creating a wrapper shell script that can be used in the #! line in other scripts.
wrap.sh
#!/bin/bash
set -e
echo "wrapper!"
exec ruby "$#"
test.rb
#!/usr/bin/env wrap.sh
puts RUBY_VERSION
puts "the ducks come from the trucks"
wrap.sh is in the path, and test.rb is marked as executable.
Now I do:
./test.rb
wrapper!
ruby: no Ruby script found in input (LoadError)
The goal is to execute the ruby script via the wrapper (The ruby version can be either local or comes from a traveling ruby install that is shipped along with the app).
As far as I can tell ruby is invoked, it's just unhappy with the #! in the test.rb and refuses to run the script. I cannot remove the #! because that's how the script is executed in the first place.
Any workarounds for this?
So, I cannot use rbenv/rvm/etc. There is more logic in the wrapper than this, but this is the gist of it.
Looks to me like the arguments are not being passed to Ruby in "$#". I don't think the bang-hash line is the problem.
I don't see anything in your script which actually passes the contents of test.rb to wrapper.sh, which is the bigger issue.
Perhaps the real problem can be solved by some other means? For example, is the problem you're trying to solve to run arbitrary commands prior to the invocation of any Ruby script from the command line? Perhaps it can be approached that way...
It looks like Ruby just checks that the hash-bang line contains "ruby": https://github.com/ruby/ruby/blob/v2_2_2/ruby.c#L1580 So basically having ruby somewhere in the #! line is all it takes.

Get newline in terminal after execution of Ruby scripts

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.

Ruby: Keep console open after script execution

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

executing shell command from ruby

This isn't working in ruby and I don't understand why?
#!/usr/bin/env ruby
exec "sort data.txt > data.sort"
data.txt is a comma sepparated file. Anyway.. If I try to execute that line in the shell it works without a problem.
When I try to run this script from my script, I get an empty file.
This isn't really an answer, but I wanted to share that your original usage of exec is actually working for me. This was how I set it up.
data.txt
"1,2,3,4,5,6,7,8"
sort.rb (I don't know what your sort did so I am just writing the same data out)
File.open(ARGV[0]){|f| puts f.read}
irb session
irb(main):001:0> exec "sort data.txt > data.sort"
When I ran this in irb, I did get a data.sort output file and it contained "1,2,3,4,5,6,7,8" as expected. I can run the same exec line through irb or from another ruby file, and I get the output file with data each time.
I am running Ruby 1.8.6 on a 32bit Windows XP system.
Have you tried
%x(sort data.txt > data.sort)

Resources