Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Methods as arguments and method schemas

Hi, I have two questions related to ruby, sorry if only specific script related questions belong here.

First, is there any way to do something like:

def action(X)
stuff
end

where X would be a method? For example:

class Array
def example(X)
self.X
end
end

array_1.example(pop)
array_1.example(push(5))

Second, is there anyway of creating a template/algorithim that Ruby/RGSS can use to create methods for a particular class; basically, methods that operate on/make new methods?

Thank you for any help :)
 

e

Sponsor

You can use class_eval and module_eval to evaluate a String as proper Ruby code in a given scope; as you might've guessed, class_eval will evaluate whatever String you pass it as a Ruby code but within the scope of the class you're calling it in. Same for module_eval.

For more, neeraj wrote a good tutorial about class_eval: http://neeraj.name/blog/articles/235

module_eval is used pretty much the same way.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top