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.
Related
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
This is an example output from Trollop, the Ruby option parsing gem
v0.0.1a
Options:
--input, -i <s>: Input file location (required)
--output, -o <s>: Output file destination (required)
--cores, -c <i>: Number of cores (default: 4)
--threshold, -t <f>: Threshold (default: 1.0)
--version, -v: Print version and exit
--help, -h: Show this message
It's the best option parser available because it's so small and neat, but I really don't like the centre justification of the help message. I have never seen this kind of output before in programs I have used and would much prefer it to align the options to the left, and then the descriptions to the left in a second column. Is it possible to make it do this?
Cheers
Edit:
In the latest version of trollop this is formatted with a left justification now. It's great! Although I did get used to the centre justification after a short time. Thanks
Nope. Trollop is now at version 2.0.0 and help formatting is still hardcoded in the code. You can hack trollop.rb if you wish to augment to output formatting.
For example:
code = <<-EOH
bundle install
bundle exec unicorn -c /etc/unicorn.cfg -D
EOH
What does this code do? What is <<- called?
It's called heredoc. An easy way to define multiline strings which may include single or double quotes without needing to escape them.
See more here, for example.
Often you use heredocs to define large chunks of code. Some editors know about this and can highlight syntax for you there (if you specify language). Look:
There is also a newer HEREDOC syntax for Ruby <<~END that more closely resembles what you would typically see in most shells and other languages with the ~ instead of the - to tell Ruby to strip the leading whitespace to match the least indented line in the block.
https://infinum.co/the-capsized-eight/multiline-strings-ruby-2-3-0-the-squiggly-heredoc
Looks to me like heredoc. The - allows the ending delimiter to ignore whitespace before it.
A simple Google Search gave me this.
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.)