joelparkerhenderson
joelparkerhenderson

Reputation: 35443

How to translate iOS Objective-C category interface code to RubyMotion?

Objective-C uses a "category" interface to add new methods to existing classes:

@interface ClassName (CategoryName)
@end

Example to add a "hello" method:

@interface XYZPerson (HelloWorld)
- (NSString *)hello;
@end

RubyMotion does not have "categories" nor interfaces in the same exact way.

How does RubyMotion code provide similar functionality?

Upvotes: 0

Views: 154

Answers (1)

joelparkerhenderson
joelparkerhenderson

Reputation: 35443

RubyMotion has similar functionality.

Use typical Ruby to re-open a class to add a method:

class XYZPerson
  def hello
    # return a string
  end
end

If you prefer to modularize the code, or to use the same code in multiple places, you can use a Ruby module:

module Hello
  def hello
    # return a string
  end
end

class XYZPerson
  include Hello
end

Be aware that RubyMotion file load order is important. For example, if you define the same method more than once, the new definition will overwrite the old definition.

Upvotes: 2

Related Questions