Search code examples
macosrubymotion

TCPServer Class NameError in RubyMotion (OSX)


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


Solution

  • 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.