Using Java big IDEs compile my code while it is written so that errors are detected before runtime.
Is that possible with Ruby too? Actually I code in a Text editor. Errors are detetected at runtime only.
Is that possible with Ruby too?
If by that you mean "compiling", then no. If you mean "edit-time error detection", then also no.
Smart IDEs, like RubyMine, can guess/detect some errors, but only simple cases. And they are often confused by ruby's dynamic nature. (can't find location for a method, even though it's defined within the project. Or the opposite, find too many false positives).
In ruby, you simply can't know what does a piece of code do without running it.
Related
I was wondering if there is a quick and effective way to remove all the unused variables (local, instance, even properties) in xcode... I am doing a code cleanup on my app and if I knew a quick way for code refactoring it would help me a lot...
Thanks...
It's being a long time since you made your question and maybe you found an answer already, but from an answer to a related question:
For static analysis, I strongly
recommend the Clang Static Analyzer
(which is happily built into Xcode 3.2
on Snow Leopard). Among all its other
virtues, this tool can trace code
paths an identify chunks of code that
cannot possibly be executed, and
should either be removed or the
surrounding code should be fixed so
that it can be called.
For dynamic analysis, I use gcov (with
unit testing) to identify which code
is actually executed. Coverage reports
(read with something like CoverStory)
reveal un-executed code, which —
coupled with manual examination and
testing — can help identify code that
may be dead. You do have to tweak some
setting and run gcov manually on your
binaries. I used this blog post to get
started.
Both methodologies are exactly for what you want, detecting unused code (both variables and methods) and removing them.
Is there a way to make emacs pull autocompletions of ruby methods the way Eclipse and NetBeans do? That is if I type File. and press CTRL-space in Eclipse I will get a list of File methods. Same with variables. I have installed autocomplete plugin, ruby-mode, rinari and cedet, but so far it will complete local variable and method names, but will not native ones.
I think you need something like RSense. You might also like the more general auto complete mode.
I'm not familiar with ruby, but if by "native methods" you mean stuff in some system library, there are a couple options for extending CEDET to do the work.
If there are ruby files somewhere that have all that code in them, and if ruby supports some sort of "include" or "import" statement, then you need to add that location to the include path for ruby. This probably requires a change the the ruby source code to add a new system include path. You can see examples in semantic-c.el. You may also need to override the function semantic-tag-include-filename to convert the include into a findable filename.
If there are no includes, and there is just some ruby interpreter that knows all this stuff, then you will instead need to code up a full ruby "omniscient" database, similar to semanticdb-el.el. It will need a way to query ruby for various things and return them as answers.
Any such enhancements would be welcome back in the ruby support in CEDET's contrib area.
Ruby is an interpreted language, making it difficult to do certain things, such as autocompletion. How would you know what the object type is, if it's not defined? Therefore, premade solutions are limited or nonexistent. Even the autocompletion in Netbean/Eclipse will only work on class methods (if I'm not mistaken).
Does anyone have any insights regarding compiling Ruby code for Windows? I've tried both "Ruby2Exe" and "OCRA", but both present their own issues. Ruby2Exe keeps presenting vague or confusing warnings such as "can't modify frozen string". OCRA on the other hand seems to want to run your script and assumes that there are no dynamic items.
For the record, my script accepts command line arguments as well as reading in and parsing a text file. OCRA doesn't like this aspect at all, and actually throws the warnings in my code as if I tried to run the script.
Anyway, if anyone has any quality means by which to compile ruby code for Windows, I'm all ears.
As a bit of an FYI, my goal with this particular script is to send email over SMTP. It is part of a larger non-ruby application, but the framework is incapable of sending email. I find Ruby enjoyable and rather easy to work with but don't wish to have every end user install Ruby -- hence, the need/desire to "compile" it.
I'm on a short time table and can't really afford to expend resources on writing this in C++, etc. However, if anyone has any insights on any existing Windows-compatible libaries/applications, do tell.
Much appreciated.
"OCRA on the other hand seems to want to run your script..."
The constant Ocra is defined at compile-time but not at run-time. So you can include logic based on whether or not the Ocra constant is defined. For example:
app = MyApp.new
if not defined?(Ocra)
app.main_loop
end
In Java when you compile a .java file which defines a class, it creates a .class file. If you provide these class files to your coworkers then they cannot modify your source. You can also bundle all of these class files into a jar file to package it up more neatly and distribute it as a single library.
Does Ruby have any features like these when you want to share your functionality with your coworkers but you don't want them to be able to modify the source (unless they ask you for the actual .rb source file and tell you that they want to change it)?
I believe the feature you are looking for is called "trust" (and a source code control repository). Ruby isn't compiled in the same way that Java is, so no you can't do this.
I have to say your are in a rough position, not wanting to share code with a coworker. However, given that this is an unassailable constraint perhaps you could change the nature of the problem.
If you have a coworker that needs access to some service provided by a library of yours, perhaps you could expose it by providing a web/rest service instead of as a .rb file.
This way you can hide your code behind a web server, and if there is a network architecture that allows for low latency making these service calls, you can effectively achive the same goal.
Trust is a lot easier though.
edit:
Just saw this on HN: http://blog.astrails.com/2009/5/12/ruby-http-require, allows a ruby file to include another file through http instead of the filesystem.
Ruby is
A dynamic, interpreted, open source programming language with a focus on simplicity and productivity.
So like all interpreted languages, you need to give the source code to anyone who want's to execute your program/script.
By the way searching "compiled ruby" on google returned quiet a few results.
I don't think there is one. Ruby is purely an interpreted language, which means ruby interprets your source code directly in order to run it. Java is compiled, so there's an intermediate bytecode (the .class). You can obfuscate your ruby if you really wish, but it's probably more trouble than it's worth.
Just to make sure you realize, however, upwards of 95% of Java can be decompiled back into source using various free utilities, so in reality, Java's compilation isn't much better than distributing Ruby source.
This is not a language specific problem and one that can be managed more effectively through source control software.
There is a library called ruby2c that compiles a subset of Ruby into C code (which you can then compile into native code, if you want).
It was actually originally written as a Ruby code obfuscator (but has since been used for lots of other stuff, including Ruby Arduino development).
We have a proprietry system that we develop scripting code in.
We currently do not have a developer environment (apart from Notepad++) and cannot debug or compile this code. We have to submit it to the vendor to insert the code into the test or live system.
The language is essentially C like and has the same syntax.
Basically we want a tool to be able to simply check the syntax of chunks of code we send to the vendor.
Does a tool exist that will do this for me?
You write code in a proprietary scripting language, so you require syntax checking because you cannot compile or debug the code onsite? I'd suggest getting a copy of the language reference (including the BNF if possible) from your vendor, get a compiler-compiler like Coco/R (http://www.ssw.uni-linz.ac.at/coco/), and build yourself a quick and dirty compiler that just validates the abstract syntax tree.
That is to say, yes, there are tools you can use, though perhaps they involve more work than what you may have hoped.
If it's really the same syntax as C you can use a C compiler. Usually there's a syntax check only option (/Zs for MSVC).
I'm not sure how many problems you'll run into since C compilers are pretty picky, and being "like C" is not the same as being C.
It does seem odd that you're being asked to develop code without having any capability to run or even compile it. Kind of like writing a book without being able to proof read it before publishing. I have a hard time getting even "Hello World" programs to compile & run without some sort of goof-up on the very first go.