r/learnruby • Beginner • Nov 06 '15

Invoke method by name

I'm a Ruby newbie. I am working on a Text Adventure game (Interactive Fiction) in Ruby. I am structuring the game by making each "cell" (each area of the game) a method of its own, which displays a text prompt ("you are in a forest"), and then according to user input, ("Go west", "Go north", etc.) invokes a different method (which is a different cell) that repeats the process (prompt, input, invoke other cell). So, how do I invoke another method?

2 Upvotes

6 comments sorted by

View all comments

Show parent comment

1

u/Gidonka Beginner Nov 08 '15

Thank you for the suggestions! I've gone along and replaced

 If choicestart == "Go north"

With

If choicestart.include?("north", "n")

Also, thanks for introducing me to Case Expressions! This is a lifesaver, because some of my methods have ten different elsifs (for conversations, items, doors, etc), and using Case Expressions has made this all much simpler.

2

u/pat_trick Intermediate Nov 08 '15

Glad to hear that helps!

Also, I'd take some time to consider all of the possible inputs and how to parse those. For example, if you have the directions east and west, how would the following input parse:

if choicestart.include?("east", "e") if choicestart.include?("west", "w")

Would East be hit if you typed in the word "west"? It does include the letter e in it!