Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Problem with "end" (?)
Index -> Programming, Ruby -> Ruby Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MrHippo




PostPosted: Tue Dec 11, 2007 10:43 pm   Post subject: 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)

Ruby:

#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[
#When'd you wake up?
print "When did you wake up? (to the hour, 24-hr time)"
wake_up = gets
wake_up.to_i


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
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Dec 11, 2007 11:25 pm   Post subject: 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
code:

if school == "yes"

Actually you don't even need this temporary variable!
code:

if gets.downcase == "yes"


I would have done something like
code:

irb(main):001:0> school_time = (gets.strip =~ /yes/i) ? 7.33 : 0
Yes
=> 7.33
irb(main):002:0> school_time
=> 7.33
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




PostPosted: Wed Dec 12, 2007 1:34 am   Post subject: RE:Problem with "end" (?)

You have a typo:

code:
  end[


Oh, and while we're suggesting alternatives...

code:
name = gets.upcase
puts "Well, " + name + " let us see how much time you wasted. Get ready!"


Can be:

code:
puts "Well, #{gets.capitalize} let us see how much time you wasted. Get ready!"
Clayton




PostPosted: Wed Dec 12, 2007 8:36 am   Post subject: Re: RE:Problem with "end" (?)

wtd @ Wed Dec 12, 2007 1:34 am wrote:
You have a typo:

code:
  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




PostPosted: Wed Dec 12, 2007 4:40 pm   Post subject: 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




PostPosted: Wed Dec 12, 2007 4:43 pm   Post subject: RE:Problem with "end" (?)

you're running it the wrong way

open up your command prompt, navigate to the folder, type
Quote:

ruby file_name.rb
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
MrHippo




PostPosted: Wed Dec 12, 2007 4:51 pm   Post subject: 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




PostPosted: Wed Dec 12, 2007 5:11 pm   Post subject: Re: RE:Problem with "end" (?)

MrHippo @ Wed Dec 12, 2007 4:40 pm wrote:
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 Razz
Sponsor
Sponsor
Sponsor
sponsor
MrHippo




PostPosted: Wed Dec 12, 2007 10:29 pm   Post subject: 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




PostPosted: Wed Dec 12, 2007 11:33 pm   Post subject: Re: RE:Problem with "end" (?)

MrHippo @ Thu Dec 13, 2007 5:40 am wrote:
Thanks Tony & wtd (although I don't fully understand the

code:
(gets.strip =~ /yes/i) ? 7.33 : 0


As your other question has been answered...

This involves a couple of components. Let's look at:

code:
gets.strip =~ /yes/i


And let's break that down a bit farther.

code:
gets.strip


And one step further.

code:
gets


The "gets" method gets a line of input from the user. The result is a String object.

code:
gets.strip


Takes that String object and calls its "strip" method. This removes leading and trailing whitespace characters. " yes " becomes "yes".

code:
/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:

code:
Conditional_expression ? return_if_true : return_if_false


So...

code:
(gets.strip =~ /yes/i) ? 7.33 : 0


Assuming the user inputs something that matches that regular expression, this will return 7.33.
Display posts from previous:   
   Index -> Programming, Ruby -> Ruby Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: