I have some code I am porting over to RubyMotion from MacRuby and I'm getting a TCPServer Name error: Here is the error message:
app_delegate.rb:29:in `control_server:': uninitialized constant AppDelegate::TCPServer (NameError)
This is the line of code that causes the problem:
dts = TCPServer.new(ip, port)
I have read that some classes from Ruby aren't included in RubyMotion but I'm not sure which classes and how to determine which classes aren't available.
Thanks
RubyMotion doesn't include the entire Ruby standard library because RubyMotion doesn't come with a built-in way to deal with certain parts of the Ruby language that are required by lots of the standard library and community gems - such as require, eval, load and so on.
Traditionally, we've dealt with this by using Objective-C libraries instead, since RubyMotion bridges Objective-C classes seamlessly. There's a wealth of existing Objective-C libraries out there and a fabulous dependency management system in CocoaPods. If you're down for a little rewriting, the CocoaAsyncSocket library should suit your needs.
If you're feeling a little more adventurous, you could try getting your existing code working by using MotionBundler. It attempts to add require support to RubyMotion, with varying levels of success. In theory, you'd simply put back that require 'socket' line in your code after you've set up MotionBundler and it should take care of the rest. I got some pretty scary-looking stack traces when I tried it myself though, so it's probably not a silver bullet. TCPServer is particularly worrying since I'm fairly sure it uses native C extensions, which are not supported by MotionBundler.
Related
That was a very surprising finding when I switched my app to Ruby3+ version.
Even though it is a major version upgrade but still..
Removal of Core data type from stdlib that's been there for at least 10 years(since 1.9.3?) looks weird.
Was there any compelling reason I'm not aware of?
The error that it raises:
"The SortedSet class has been extracted from the set library. You must use the sorted_set gem or other alternatives."
After a bit of searching I found the following:
Remove SortedSet implementations PR
It required RBTree to perform decently and the external dependency was not suitable for a standard library. The pure ruby fallback implementation was originally meant to be a demonstration of how to write a subclass of Set, and its poor performance was not suitable for use in production.
I decided it should be distributed as an external library instead of bundling it with Set.
To keep it short:
SortedSet has been removed for dependency and performance reasons.
source
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.
I feel that a native extension is like libraries that you should install onto your system before trying to install those gems, which depend on the native extensions. Like the ImageMagic library. Is that correct? Is there something else that we should know about native extensions?
A gem native extension might link to a separate library that needs to be pre-installed, and RMagick is an example of that.
However, the term really just means "includes some code that needs to be compiled for your machine". The code is compiled and linked so that the resulting executable file can be required by Ruby on the machine.
The usual reasons for writing a Ruby C or C++ extension are:
Speed. For some CPU-intense tasks, C code can be 100 times faster than Ruby. In this case a native extension can be completely stand-alone with all C source code included in the gem.
Third-party library already written in C. In this case the gem will have C source code that binds the library functions into Ruby modules, classes and methods.
You can view the C source code for gems with native extensions, it is installed alongside the Ruby source code. By convention, there is a folder inside the gem called ext/gem_name which contains a Ruby file extconf.rb that passes information to the compiler (technically it creates a make file). In addition the C source files are put there as well.
MRI Ruby is implemented as a very "flat" structure in C, basically consisting of a large number of C functions. This makes it relatively easy to learn how to implement a native extension, even if you don't know much C. You can read Extending Ruby 1.9 as an introduction to the topic.
Native extensions can fail to install or work properly. There are many questions on Stack Overflow asking for help with specific failed installs. The usual problems are:
Missing libraries. Hopefully the gem author will explain what you need to pre-install in the README, but is not always clear.
Compiler mismatches. It is difficult to test all target systems, so sometimes the instructions in extconf.rb won't work on a specific system, or the C code will raise warnings or not compile due to differences. In Windows, you probably won't have a working compiler unless you install the Ruby Devkit
Will not work with all versions of Ruby. For instance, JRuby can use C native extensions, if it has been enabled but it is not always advisable - the topic is quite complex, but generally avoid mixing JRuby and native extensions.
Native extension is just a gem which is written (entirely or partially) in C.
It may or may not depend on an external library, this is not a factor here. What matters is that such gem needs to be compiled and it is likely to be platform-dependant (there was a reason to use C, right? Maybe for using some low-level OS API or something. But most often it's to interface with a library).
Quoting this article
“Native extensions” are the glue that connects a Ruby gem with some other non-Ruby software component or library present on your machine.
The native extensionis not the dependency. A native extension is generally a C code that interacts with a non-Ruby dependency.
For instance, a gem that uses ImageMagic have a native extension written in C that talks to ImageMagic and represents the bridge from the Ruby gem to ImageMagic.
When you install the gem and the native extension is compiled, you don't compile the C library (e.g ImageMagic), that library must be already present on your system. You compile the C bridge bundled with the gem.
I'm not big ruby specialist, so take this with a grain of salt:
I'm fairly certain that it's just a gem that needs to install a native (e.g. C-Library) library in order to work. A lot of gems simply wrap existing C-libraries with a Ruby API.
The installation of the gem will trigger the download of C-libraries, which will then be built using gcc or another compiler. If your system configuration is not supported, you'll need to pass parameters to gem tool to indicate the right directories, etc.. if you're unlucky you might need to change the make files directly.
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).