How can I print syntax-highlighted Ruby code? - ruby

I use TextMate for my Ruby editing, but when printing files, the code isn't syntax highlighted. Are there any good programs for printing out well-formatted color-highlighted Ruby code?

A good solution which I use is to print from TextMate via vim which gives you a syntax-highlighted and line-numbered result (or however you choose to configure it.) In addition to vim it requires ps2pdf but these are easy to install with macports etc.
The only limitation is that the file needs to be saved first.
See this page, which shows how to set up the macro as a TextMate 'command'.

I don't know if it's relevant in Mac-world, but the SciTE editor that's bundled
with the one-click installer for Windows prints nicely in colour on our HP Laserjet. I haven't tried printing from any of the various IDEs - I ought to try it.

I'd probably use Ultraviolet to create an HTML file that's syntax-highlighted to print out...

The syntax gem is pretty awesome.
require 'hpricot'
require 'syntax/convertors/html'
def filter_content(content)
h = Hpricot(content)
c = Syntax::Convertors::HTML.for_syntax "ruby"
h.search('//pre[#class="ruby"]') do |e|
e.inner_html = c.convert(e.inner_text,false)
end
h.to_s
end
Edit: Oh, you are referring to printing... Well you could do convert it to HTML and then print it from your browser.

Apparently it is difficult to communicate to the printer that a *.txt-equivalent file needs to be colorized but it obviously can be done. TextMate's creator has communicated simply that he doesn't think it is important enough of a feature to have which basically means it is extremely difficult to implement.
Aptana can do this thing no problem but it takes forever to load.
Enter vim. vim is probably already installed on your computer.
vim filename.m
:syntax on
:hardcopy
Again, there won't be any prompt for which printer to use, so make sure your system's default printer is set correctly.
If you had to use the :syntax on command to get vim to colorize your code:
To set vim by default to do syntax-based coloring:
nano ~/.vimrc
syntax on
save & quit

Thanks, boulder_ruby
A couple of additional points at the end of 2016 (2 years 10 months later):
If you're on Windows, you cannot count on VIM being already installed. Membership in Stack Overflow does improve the odds, but probably not to 90%). However VIM for Windows is easy to get. www.vim.org/download.php
Macs come with MacVim by default.
On a Mac, using MacVim, following your advice, I carefully set the default printer to one where I could empty the queue before wasting paper on testing.
Then I used the :hardcopy VIM command. The printed version went straight to PDF and opened in Preview. It never showed up in the default printer queue.
Also, edavey above points to http://biztos.blogspot.com/2008/06/printing-with-textmate-vim-and-friends.html which contains a link to the TextMate Help page on printing. The TextMate help says:
There are plans to improve the printing capabilities, but until then,
there is also a command in the Source bundle (View Source as PDF)
which produces a PDF from the current source using enscript and has
syntax highlighting enabled for supported languages.
That suggests (I haven't figured it out the "Source bundle" yet) that you can go straight to PDF, with syntax coloring, from TextMate. If I figure that out, I'll add something here (eventually).
I hope this helps.

Related

How Ruby Is Run

I come from an object oriented programming background, and have picked up Ruby as a hobby. It looks like a great language. My questions are:
Can you use any ol' test editor, write your ruby file, save it with a .rb extension and open it in the terminal?
Is this the most common method of using Ruby (Or other script languages), rather than with an IDE?
How do I close external programs on my Mac with ruby? What I will be doing with Ruby has a lot to do with opening files and commanding them. I was using AppleScript but want to convert. I've tried:
system open "John/Applications/TextEdit.app"
And it didn't close.
I'm on Windows but I'll try to answer your questions as good as possible, most of the stuff is OS independent.
For questions 1 and 2: You can use one of the following ways to edit and run Ruby scripts.
IRB (Interactive RuBy shell included with Ruby)
websites like http://tryruby.org/levels/1/challenges/0
An IDE such as:
RedMine
KomodoEdit
Eclipse with plugin (not that easy to configure)
not really necessary because Ruby code is short and easy to remember, kind of pseudocode, the advantage is in the beginning when you don't know the commands and structures and for debugging
voordeel = debuggen is makkelijker
Editor with a Run-optie like Textpad, Notepad++, Sublime Text..
The last one is my favorite, there are packages for Ruby but Ruby support is in the basic installation, you can edit your code with syntax-coloring and suggestions, run your code and the result is captured in a separate tab. It is the most widely used way of Ruby coding also.
In Windows I use the following way to run external programs and capture the result. I believe it to be working on a Mac also. The external shell and program is closed after the last end.
answer = ""
command = %Q{java -jar test.jar #{$parameter1} #{$parameter2}"}
IO.popen(command+" 2>&1") do |pipe|
pipe.sync = true
while str = pipe.gets
answer << str
end
end
#the answer variable holds all the output lines

Forth editor/customizable editor

I'm looking for an OS X editor (preferably a dedicated Forth editor, but I doubt it exists) that has/can be customized to change the font color of Forth variables, programs, etc. If not, is there any sort of workaround?
I know it's an old question, but: atom with the forth-language package. See http://atom.io/
Longer answer:
I have tried both emacs and atom on Mac OS X and both are more than adequate for editing Forth. Atom is lighter and easier to learn while maintaining many emacs-ish capabilities (cmd-shift-p in place of meta-x).
I don't have automatic indenting working in atom yet, the syntax package I found needs a little work, but this hasn't been a real problem as yet.
atom also plays well with git.
I'm using the following packages: emacs-plus, language-forth, clipboard-plus, disable-arrow-keys. The key and clipboard are for a more emacs like experience. There are more themes and color schemes than I need available, I'm using the 4-color-dark and minimal-syntax themes.
UltraEdit has a Forth syntax highlight mode. MicroProcessor Engineering have an up to date copy in there downloads page.
Look no further !!
I'm currently programming in FORTH using the 'Visual Studio Code; editor.
Has (installable) syntax-highlighting for many languages, including FORTH.
Have a try:
https://code.visualstudio.com/download
It has even automatic indenting, AND code folding!!
...and let me know if you like it like I do.
Robbert / PA3BKL
I would suggest Gedit and if you change the forth.lang in the app you can change the syntax highlighting

Vim hide tear-off menu on Windows xp

My problem is I'm trying to hide the tear-off-menu for VIM on Window XP(Sp3). I just don't like it. How can I do that. Please help.
Just as a suggestions I'm asking the below mentioned question -
I'm learning to use the VIM editor for Windows, mainly the GUI version. Then I was pointed to learn from this blog - Vim Novice Tutorial Videos. Now the tutorials are good but there are practically no tutorials for VIM on windows. Can anyone point me to some windows tutorial for vim.
Another thing is that, I was trying to use the vimrc file of this user but was unable to setup plugins for windows.
set guioptions-=t
in your _vimrc will hide the tear off menu items in the future.
Regarding the tutorials (Vim/GVim on Windows), no, they aren't usually divided between Vim on Unix/Linux and Vim on Windows, Vim being rather similar on all of those. Some things differ (for example, the name of your vimrc file which is customary _vimrc on Windows, and .vimrc on the *nix boxes), but users usually pick those quickly and forget about them.
Regarding the vimrc file and the plugins found on that link, what is your directory structure? Where did you put vim runtime files and where did you put these configuration files in regards to vim's position? Could you tell what errors (problems) did you encounter?

Simple Ruby Editor?

This editor isn't intended for me. It's intended for teaching someone else Ruby. I largely use Emacs when writing Ruby. Does anyone know of an editor that
indents the current line correctly when you press tab,
can indent the whole file correctly (keyboard shortcut would be nice),
has syntax highlighting for Ruby,
other than that works kind of like Windows Notepad (maybe with a toolbar of icons),
has a built in terminal (not absolutely required),
has multiple editor tabs,
and works on Windows, Linux, or both?
Or anything somewhat close?
Edit:
I'm also going to add that I'd like to be available for free (legally for an indefinite period of time). Open source wouldn't hurt.
Check Notepad++
I don't think that jEdit has a built-in terminal, but I believe it does everything else you need. It's implemented in Java, so it should run on any platform for which a JVM exists, including Windows and Linux.
Since it's about teaching ruby RedCar and Diakonos are good choices. They have most of the features you mention and as a bonus both are written in Ruby.
If you are not a die hard fan of emacs, then there's always vim and its plugins for any programming language. Here's one for Ruby: rails.vim : Ruby on Rails: easy file navigation, enhanced syntax highlighting, and more
If you are new to vim, this cheat sheet should help you: vim graphical cheat sheet
I like gedit - it doesn't have a terminal, but should work fine for the other requirements. It can run on Windows with Cygwin.
For a full-blown IDE, Aptana Studio is great. It meets your requirements.
If your main goal is to teach then you could try using Hackety Hack. It is somewhat weak at times, but I found it fun and simple to use, but then again I already knew ruby pretty well.
For a beginner being able to run the programs easily is very important so you want something where irb and ruby can be run very simply. Aptana seems to be a pretty good IDE for this sort of thing, but I have never used it myself aside for web development.
Aside from those two I suggest Notepad++... Also the free KomodoEdit might be worth a look. Personally I say teach the other person emacs, but then again shrug

Ruby and linux, preferred setup?

Mac's have TextMate as there preferred application for ruby development, but what would be the preferred application for linux? I need something where it's easy to work with multiple files, project structure and setup commands to run my ruby app or if it is one my merb app.Syntax highlighting is also a must.
Now I typically use Vim, but it's not the best for working with multiple files or with a project structure, even with VTreeView plug-in or multiple VIM windows.
So what would you guys suggest?
If you have better plugins to use for VIM feel free to mention them, I'm not ruling out VIM here.
I use Vim on both Windows and Linux for development in Rails (we have to use Windows in work, and I only use Linux at home). The environment is almost exactly the same for both platforms. Especially important for me is easy navigation between the various Rails components - from Controllers to views, partials and models, and quick navigation to test files.
Here are the plugins I use:
Vim Rails by Tim Pope. The :R, :A and gf commands are the ones I use mostly for navigation.
NERDTree for a project/explorer view.
NERDCommenter for easy multi-line commenting.
FuzzyFinder and "FuzzyFinder - Textmate" - allows you to quickly find files based on portins of a file name.
Ctags
Bufexplorer
dbext for executing SQL commands and getting the results in a Vim buffer.
Ack and the ack plugin for a better grepping experience from within Vim.
VividChalk colour scheme.
RubyMine from JetBrains.com works well for Mac/Linux/Windows, the price is 99$ but it's probably the most productive IDE for Ruby and Rails I have tested so far.
Setup Gedit to be almost like Textmate
Aptana with the Rails plugin is pretty good.
If you are on Ubuntu/Debian, plain old emacs with ruby-elisp package isn't bad. It's no TextMate, but it's not bad.
I prefer Netbeans on both linux and Mac
+1 for Netbeans for Rails. Each release gets better and better and with 6.7 beta it's better yet. Using it on Windows and Mac -- under Linux it's what I'd use as well.
Since you are a vimmer, have you looked at this? I have no experience with it, but looks quite good in the screencast.
Why don't you just use Sublime Text 2 Text Editor, it is free and cross platform and lighter than any IDE, and then you can install the SublimeCodeIntel which will provide you with autocomplete features , you can do that through installing Package Control , then
⌘+shift+p → “install” → ENTER → “codeintel” → ENTER → Restart ST2
It is working perfectly with me and I'm totally in love with this smart editor .
You can find this helpful somehow if you wanna give it a try , http://www.rockettheme.com/magazine/1319-using-sublime-text-2-for-development
I'm using it with zsh Terminal http://stevelosh.com/blog/2010/02/my-extravagant-zsh-prompt/
Anyways if you are looking for a full IDE give netbeans a try http://netbeans.org/projects/ruby/
I prefer Aptana/RadRails on both Mac and Linux. It gives a consistent experience for me no matter what OS I'm on.
I still don't get the excitement over Textmate...
Given that you use vim, this post might be interesting.
Her is the Fuzzyfinder Textmate vim plugin that the post refers to.
Aptana Studio is indeed very nice. Also Gedit does the job if you don't want a full IDE environment and are more inclined to do stuff by hand :).
The other answers are about ruby editors, so I thought I will add an answer on my linux setup.
I use Ubuntu with VM player (free) on top of windows 7. I dedicate 2 core and 2 GB to the vm. Benefit of using the VM on top of windows is that I can use linux just for development and windows for everything else. Skype, webex, and team viewer works in windows, but i find them to be flaky in linux. Also I use office once in a while, very easy on windows.
I have been using GMATE for a while and I can say that I only need gnome-terminal to complete my ruby/groovy/python setup. It have themes imported from textmate and do some method/property code completion (not much ok? but it comes handy).
Edit: forgot to say that GMATE is a set of plugins for Gedit (default text editor on gnome)
With Linux there are 3 really good IDEs and all are free. You have Eclipse, Netbeans, and Aptana... They are all very good and each have some benfits over the other, its more a matter of preference. I would suggest downloading all 3 and giving them a try to see which you prefer.

Resources