----------------------------------- MrHippo Tue Dec 11, 2007 10:43 pm Problem with "end" (?) ----------------------------------- Well, after about 2-3 days of learning Ruby I decided to try to write a simple program to determine how much time someone wastes in a day. However, what happens is that whenever it gets to an "end" method, it simply shuts down =S Is there something wrong with the code or with the way I run it? (I don't know how to post the code in the proper format, so I'll put the excerpt where the problem occurs first, though if I remove that part it occurs at other "end"s) #Start the input #Intro puts "Hello, and welcome to TimeWaste!" puts /n puts "Please enter your name:" name = gets.upcase puts "Well, " + name + " let us see how much time you wasted. Get ready!" #School? print "Did you go to school today? (yes/no)" school = gets.downcase if school = yes school_time = 7.33 else school_time = 0 end So it works fine up to the school question. After that, no matter what I put, it exits :S Any help'd be appreciated!(I've been trying to properly arrange, configure and work with "end"s for a while just 'cause of this stuff x_x) MrHippo ----------------------------------- Tony Tue Dec 11, 2007 11:25 pm RE:Problem with "end" (?) ----------------------------------- first of all, = is assignment, == is comparison. And yes doesn't hold a value, you've probably meant a string liberal. So it should have been if school == "yes" Actually you don't even need this temporary variable! if gets.downcase == "yes" I would have done something like irb(main):001:0> school_time = (gets.strip =~ /yes/i) ? 7.33 : 0 Yes => 7.33 irb(main):002:0> school_time => 7.33 ----------------------------------- wtd Wed Dec 12, 2007 1:34 am RE:Problem with "end" (?) ----------------------------------- You have a typo: end[ Oh, and while we're suggesting alternatives... name = gets.upcase puts "Well, " + name + " let us see how much time you wasted. Get ready!" Can be: puts "Well, #{gets.capitalize} let us see how much time you wasted. Get ready!" ----------------------------------- Clayton Wed Dec 12, 2007 8:36 am Re: RE:Problem with "end" (?) ----------------------------------- You have a typo: end[ That was me, he originally had that section bolded, as opposed to in code tags. I forgot to remove the last bracket I suppose. ----------------------------------- MrHippo Wed Dec 12, 2007 4:40 pm RE:Problem with "end" (?) ----------------------------------- Thanks Tony & wtd (although I don't fully understand the (gets.strip =~ /yes/i) ? 7.33 : 0 My bigger concern is that when I try to run ANY program it seems (even this very simple one I made as a test), it closes the window prematurely. Is there any way to stop that from happening or am I just running it the wrong way (I just go to the folder and double click the *.rb file) ----------------------------------- Tony Wed Dec 12, 2007 4:43 pm RE:Problem with "end" (?) ----------------------------------- you're running it the wrong way open up your command prompt, navigate to the folder, type ruby file_name.rb ----------------------------------- MrHippo Wed Dec 12, 2007 4:51 pm RE:Problem with "end" (?) ----------------------------------- Thanks a lot =D *smacks self on head* Wow, lots of mistakes in my code x_x Must fix.. but that's how we learn hehe. Thanks again! ----------------------------------- Clayton Wed Dec 12, 2007 5:11 pm Re: RE:Problem with "end" (?) ----------------------------------- My bigger concern is that when I try to run ANY program it seems (even this very simple one I made as a test), it closes the window prematurely. Is there any way to stop that from happening or am I just running it the wrong way (I just go to the folder and double click the *.rb file) Just to clarify, what's happening is that when you double click the *.rb file, the code is interpreted and executed. However, after you input, everything outputs, and the window is closed. The program executes so fast, you pretty much will never see what it said. So your code is still being executed, just too fast for you to see :P ----------------------------------- MrHippo Wed Dec 12, 2007 10:29 pm RE:Problem with "end" (?) ----------------------------------- *nod* That's what I kind of suspected, but I didn't think of another way to launch it so I reverted to the idea of something being wrong with the code x_x All works now though... well, not the program, but the launching part ;D ----------------------------------- wtd Wed Dec 12, 2007 11:33 pm Re: RE:Problem with "end" (?) ----------------------------------- Thanks Tony & wtd (although I don't fully understand the (gets.strip =~ /yes/i) ? 7.33 : 0 As your other question has been answered... This involves a couple of components. Let's look at: gets.strip =~ /yes/i And let's break that down a bit farther. gets.strip And one step further. gets The "gets" method gets a line of input from the user. The result is a String object. gets.strip Takes that String object and calls its "strip" method. This removes leading and trailing whitespace characters. " yes " becomes "yes". /yes/i Is a regular expression. You can think of it as a pattern. The pattern itself is surrounded by slashes. Think of them as a weird sort of quotation mark. In this case, that pattern is a very simple word. The trailing "i" modifies the regular expression to be case-insensitive. So it'll match "yes", "Yes", "yeS", etc. The "=~" operator matches a string against a regular expression. If it matches, this expression returns a number identifying where the match starts in the string. If it doesn't match, it returns "nil", which is considered false by Ruby's conditionals. Now you get to the ternary expression which follows the general pattern: Conditional_expression ? return_if_true : return_if_false So... (gets.strip =~ /yes/i) ? 7.33 : 0 Assuming the user inputs something that matches that regular expression, this will return 7.33.