Command to travel to a "paragraph"?

Posted 02 February 2011 - 10:04 PM

hopefully the title makes sense, if it does, im simply wondering what the command is, and what i need as a marker to jump to there.

for example, I have a menu with about 4 different options to "go to" another menu. i want to be able to jump to each menu regardless of where it is in the code.
...
best way to for me to explain this is to show an example and hope someone recognizes something like it.

right now, if i type anything other than 1,2,3, or 4 it says "Sorry, could you please re enter your decision?" and continues the script normally, causing you to start the game broke. Which is a big issue.

i believe if i can get this "goto" thing to work, i could simply put a "page" or "paragraph" number/name above each paragraph, and put "goto page##" underneath "puts 'Sorry, could you please re enter your decision?'"

lastly, im very sorry if I'm spamming this forum, not my intentions, if it would be prefered i have no problem opening a single thread and continually post my questions in there. all i really have is you guys for support, or other forums, i have no one around me who knows any sort of coding, and i find it very hard to sit down and read a guide and remember anything i just read, i need to do it hands on and take my own notes in order to retain any knowledge.

Replies To: Command to travel to a "paragraph"?

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 12:06 AM

It sounds like what you want is a method. Methods encapsulate chunks of code and separate them from the rest of the script, then methods can be called from other parts of the code, and the code in a method won't be run until it's called. They can take a number of arguments, and will return a single value (which may be nul). Here's an example:

def add(number1, number2)
number1 + number2 #This will "fall off" and get returned to whatever called the method
end
def map_page!()
do stuff for the map page
end

Then, you can acces your method by calling it's name and passing what values you want as arguments, separated by commas.

sum = add 1, 2 #sum now equals 3
#Or of you don't need to use the value that get's returned, just call it.
map_page

That's not nearly a complete explanation, but hopefully it can get you on the right track. Lot's of early languages included a goto command kind of like you described, but it has been mostly abandoned because separating different parts of code into methods is way easier to keep track of.

As far as spamming the forum goes, we like to keep it one thread per issue, and since you're following the rules and not just begging for "teh codez" I think you should be fine.

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 04:32 PM

I'm sorry, still quite new at this. I understand the theory. "This chunk i will name chunk 1 so i can bring it back later in the program"
But im completely lost looking at the code, and the examples.
The usage of the word "Method" is also throwing me off. In my tutorial he compares Methods to verbs.
What I'm looking for, is what is the Method i need lol.
the "goto" command for ruby.

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 04:42 PM

So... no goto command for ruby. What you're doing when you create a method is that you're creating your own commands. The "go to this section and execute this code" is implied when you call the name of the method.

See, there is a chunk of code that asks the user how much cash he wants and returns the number. If the user doesn't give an acceptable answer the code asks again and returns whatever the new answer is.

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 05:33 PM

It's not that you can't change variables, it's that you have to be careful which variables you're changing. What you're running up against here is called scoping, basically a variable is defined in certain areas of your program, what's called the variables "scope". Right now the variables that you're using are called local variables, and when they're created inside a method they only exist inside of that method (and a new one is created each time the method is called). Local variables all start with a lower case letter. Another type of variable is called an instance variable, and it starts with the '@' symbol. Instance variables scopes are limited to instances of objects, and you'll learn more about them when you get to objects. For right now you can know that when you use a variable that starts with a '@' it will be available inside and outside of a method.

For the way your program is structured right now, It's considered a good practice to use local variables as much as you can. Like methods, scoping is used to help control the complexity of your program because it's easier to keep track of chunks of local variables than large numbers of variables that can get changed anywhere in your program. Since you're still a beginner, I wouldn't sweat it too much, but keep it in mind because it'll become more important as your programs become more complex.

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 07:16 PM

okay, so adding @ in front basically makes it a universal variable. cool

last question for ya, for now anyways, in the code you gave me in your second response, could you try to explain to me how the script still ran the "questionnaire" when you had Return's in it, while mine without i need to tell it to execute that method?
and the cash = get_starting_cash line
does the result of that answer turn "def get_starting_cash" into say, variable1 = (the integer)50000.

Re: Command to travel to a "paragraph"?

Posted 03 February 2011 - 08:05 PM

Oh, I forgot to explain the return, right.

So, all methods in ruby return a value, either explicitly by using the return keyword, or the value of the very last line sort of falls off the end. If you use the return command, the method stops right there, and nothing else is executed. When a value is returned, you can think of it as replacing the method call with the returned value. If you don't use or save the returned value then it just goes away.

def do_something
while TRUE # can't remember if a do goes here
MainPage
puts '1. Inventory'
puts '2. Stats'
puts '3. Map'
variable1 = gets.chomp
if variable1 = '1'
goto InvPage
else
if variable1 = '2'
goto StatPage
else
if variable1 = '3'
goto MapPage
else
puts "Please make a different selection.'
#### ALSO I NEED TO KNOW WHAT GOES (presumably) HERE IN CASE OF THIS PROBLEM. PROBLEM BEING ENTERING AN INVALID CHOICE.
end
end
end
end # this end is for the while loop, didn't feel like inserting spaces..

PS: I have not tested any of this code, so don't expect to be able to copy and paste