How Ruby Is Run - ruby

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

Related

Tcl tclsh Set Up

I am just starting out in Tcl and am trying to properly configure an editor and interpreter for use. On Tcl's download page it is recommended to use ActiveState's ActiveTcl, but I am learning Tcl for possible production use and want to avoid paying for it.
I tried setting up Eclipse's DLTK to use for learning, but from what I found I need to direct it to the tclsh executable to use it as the interpreter. In Tcl version 8.6.6, I can't find anything that will work. The supposed formats (I'm on Windows) should follow what's seen here, as I've seen this a few examples/tutorials.
As a last resort, I tried following the Windows instructions here to use my MS Visual Studio 2015 work with this, but there is no such vcvars file (what's asked for when reading makefile.vc after downloading Tcl).
I would really like to use Eclipse for this, so I guess my question is as follows: How do you use tclsh as a Tcl interpreter in Tcl 8.6.6?
ActiveTcl isn't a pay-for product. (ActiveState have some of those, but their language distribution isn't one of them as long as you're not after a support plan.)
To configure Eclipse to use the right installed version of Tcl, go to the Tcl → Interpreters page in the main Preferences panel. That lets you Add… an interpreter, which essentially just requires you to find where the tclsh you want to use is on your filesystem, and to give it a name. I use names like “Tcl 8.6” in my installation, so it isn't exactly rocket science. The DLTK system can find out pretty much everything else once it knows where the interpreter implementation is, but if you have several set up (e.g., I have both 8.5 and 8.6) then you'll need to say which one is default using the checkboxes in the list in the Interpreters pane.
(I've verified the above against Eclipse Neon running on OSX, but I don't think it will vary very much with other versions or platforms.)

Vim's Ruby option is not enabled

I just started learning Ruby and I ran this command:
vim --version
The output looks like this (look at the yellow circle):
Which means that my Vim does not include the 'ruby' function.
But it seems my Vim supports Ruby fairly well:
q1: Is there any problem with my Ruby programming with the -ruby?
q2: How can I enable Ruby function in Vim?
The functionality on the second screenshot is given by installing the vim-ruby plugin.
The internal Ruby support is to allow people to write plugins and scripts for Vim using Ruby and it's not strictly necessary for what you seem to want to accomplish.
If you REALLY want to add Ruby support, you can always build Vim from source (or get a package with it already built). I suggest you to look into Vim's homepage for more info on this matter.
The difference you are missing is between using Vim to program in a language such as Ruby, and using a given language to program Vim.
Writing Ruby code using Vim is enabled by Vim's native syntax files, as well as various third-party plugins available for Vim. This is the case for many, many languages, not only those mentioned in the :version screen (Perl, Python, and Ruby). You can use Vim to write code in C, Scala, PHP, Javascript, and many other languages, with support for syntax highlighting, smart indenting, and so on.
Writing Vim functions and plugins can only be done in a small set of languages. Natively, Vim code is always written in its own language, Vimscript. However, Vimscript is notoriously difficult to deal with, and most people will not already know it. So Vim also has the ability to use plugins written in other languages, such as Python or Ruby. This enables people to use a more familiar language for plugin development.
However, using those languages requires binding to an interpreter for the language, and this must be decided at the time Vim is compiled. The :version screen is telling you that for your installation of Vim, the Ruby support was not enabled, so you can not write plugins using Ruby, nor can you use any available plugins which were written in Ruby.
Notice that +python is present, so you can use Python plugins (but -python3 is there too, meaning that you do not have Python 3.x support built in).

how to use ruby code after its written, is it standalone or need to be in a web application?

Confused a little on ruby. I know it makes .rb files, but does it make exe or com files or is it just used as as web application?
I know a bit writing the code, but what to do with the files after.
the question is a bit too broad.
you have to step back and look at how source code in general ends up being executed (i.e. it is used).
In case of some programming languages (e.g. C/C++) it's compiled to a native form and can be executed directly afterwards;
In case of other languages it's compiled to an intermediate form (e.g. Java/C#) and executed by a vm (jvm/clr)
In case of yet other languages is interpreted at runtime (e.g. Ruby/Python).
So in the specific case of Ruby, you have the interpreter that loads the rb files and runs them. This can be in the context of standalone apps or in th e context of a web server, but you almost always have the interpreter making sense of the ruby files. you don't get an executable the same way as the you get for languages that are compiled to machine code.
Normally you just run the .rb file in the shell or command prompt. For example in the windows command prompt:
C:\path\> ruby filename.rb
This will execute the filename.rb file from the command prompt.
If you need to run a ruby program on a computer without a ruby installation there are a few options out there.
Try this website:
https://www.ruby-toolbox.com/categories/packaging_to_executables
I personally have used OCRA to pass a program to relatives who are less computer literate. It was pretty straight forward,I haven't tried the other tools.
Good luck,

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

How can I print syntax-highlighted Ruby code?

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.

Resources