If I run code like this, I get - in the __FILE__ var:
cat script.rb | ruby
I can't find that documented anywhere though (docs). Where can I get exact details about it?
- is sometimes used to refer to STDIN. This is a unix thing, and not unique to ruby.
Described a little bit here
Related
I'm trying to execute a Ruby script file.
Assuming the input is a string that contains the file content.
What are the possible ways? taking into considerations that I need to keep the output of the executed file whether stdout or not separated from the Main script.
As an example of what I'm trying to do is have a function called execute(code)
Then calling execute('4 + 5') would return 9 although I can write a whole Ruby script in the place of '4 + 5'.
If anyone can forward me to any related tutorials or books, I'd be thankful :)
You can call shell commands in Ruby, it's as simple and intuitive as surrounding your desired command in backticks.
The output gets returned, so just save it to a variable:
script1.rb:
puts "asdf"
script2.rb:
output = `ruby script1.rb`
puts output
"asdf"
I question what exactly it is you're trying to do, though. Because this is totally unintuitive and roundabout. Are you sure you aren't just looking for a module or something?
As an exercise to learn Ruby, I would like to create a script that will be run from the terminal. It should accept as input either a string or a text file and it should output the result of various string parsing mechanisms that I will write myself.
To get me started, would you please translate this pseudo-code into proper Ruby for me?
In terminal: ruby myscript.rb (either a string or a text file).
In myscript.rb: Retrieve input. Set my_input to the input.
Set my_output to the result of various_string_parsing_voodoo (done to my_input).
puts my_output
I intend to actually write the code myself, but if someone could supply me with a skeleton .rb file to send in "Hello World" and get "[World] is pleased by your [hello]" or something equally inane that'd be a great help.
Here are some key pieces:
ARGV is an array containing the arguments you passed when running your script from command line.
the File class contains several utilies. For example, File.exists?(path) returns true if the path exists, and File.file?(path) returns true if the path exists and is a file (not a dir).
I think this may help you quite a bit.
I want to be able to get colours for Ruby (or C# or F#) in the shells.
wirble does it nicely with IRB, but I want to be able to do:
> cat rakefile.rb | colorize
Does somebody know how I could do this? I know that github's language parsers are OSS - could they be used to read lines one by one and colourise them?
pygments does that for you: http://pygments.org/docs/cmdline/
You can try source-highlight as well.
I'm using pry and I want to capture, and work with output of a shell command.
For example, If I run
pry(main)> .ls
I want to get the list of files into an array that I can work with in Ruby.
How can I do this?
This is a pretty old question but I'll answer it anyways. There are two primary methods of getting data out of pry commands. The first is if the command sets the keep_retval option to true, which the shell command does not. The second, is to use the virtual pipe. In your example this can be done as:
fizz = []
.ls | {|listing| fizz = listing.split("\n")} # can also be written as
.ls do |listing|
fizz = listing.split("\n")
end
I assume it's some kind of pry's magic ;-)
After quick look at what's happening (I didn't look at pry's source), you might want to use this:
`ls`.split("\n")
or
Dir['./*']
What's good about this solution is that it will work outside of pry
$: = '/users/joecool/rubylib'
$: << '/users/joecool/rubylib'
$:.unshift('/users/joecool/rubylib')
ruby -c somescript.rb
ruby -e "puts 'Hello, world!'
Can someone direct me to some reading, so that I can figure out what this code does?
$: = '/users/joecool/rubylib'
Sets the load path to that string.
$: << '/users/joecool/rubylib'
Adds that string to the end of the load path array.
$:.unshift('/users/joecool/rubylib')
Adds that string to the beginning of the load path array.
ruby -c somescript.rb
Checks the syntax then exits.
ruby -e "puts 'Hello, world!'
Runs that Ruby snippet. See this reference and the man page.
For general Ruby workings, look at the Pickaxe book (The Pragmatic Programmer's Guide).
For reference about Ruby objects and functions: http://ruby-doc.org/
For the precise questions, you may want to take a look at a list of predefined variables.
$: is the load path, an array containing directories where libraries are searched for. It's a less readable version of $LOAD_PATH.
For arguments to the interpreter, you may want to look at the Unix manpage for Ruby (use 'man ruby', or look at http://linux.die.net/man/1/ruby if you don't have a Unix system in handy).
Specifically:
-c checks the syntax of the script without running it.
-e takes a string that is used as the script, instead of reading the script from a file.
More extensive reading: http://www.ruby-lang.org/en/documentation/
It looks like you need an introductory book about Ruby. There are many, but I would recommend that you take a look at Beginning Ruby by Peter Cooper or Programming Ruby by Dave Thomas. (Those two are different enough in style and organization that one or the other is likely to fit you reasonably well.)