From: 7stud -- Date: 2009-09-10T08:41:44+09:00 Subject: Re: Tutorial challenge program help Chris Logan wrote: > Josh Cheek wrote: >> On Tue, Sep 8, 2009 at 8:13 PM, Chris Logan >> wrote: >> # it sets it into the instance variable @input >> # we use an instance variable because it will continue >> # to exist outside the method. You know it is an instance >> # variable because it begins with an @ symbol. If it did not, it >> # would be a local variable, and "die" after the method returned. > I do not fully understand this. I suggest you not try to understand. I would never write code like that, and I suggest you never write code like that either. >> # the last line of a method is always returned, so in this case, >> # we set the variable @input, and return it's value >> # this is because the assignment operator, =, is itself a method >> # which returns the value that was assigned. >> def get_input >> print "Say to deaf grandma: " #prompts the user for information >> @input = gets.chomp #set @input, return it's value >> end> >> # p "did not shout".shouted? >> # p "DID SHOUT".shouted? >> # when we put a question mark on the end of a function or method, >> # it indicates that a boolean value will be returned. A boolean >> # value is just true or false. >> class String >> def shouted? >> self == self.upcase #returns true if the string is uppercase >> end >> end > Oh wow, I understood this that is really cool. Don't get too enamored of that either. That's derisively called "monkey patching", and monkey patching is another bad habit that will make your code hard to read and understand. >> puts "NO, NOT SINCE #{get_year}!" #embed the year in the string > else > Do I use the # symbol? I read that that denotes a comment and anything > after it would be ignored by Ruby. > Yes and no. In this case, no. The # symbol in that line of code is part of the syntax for what's called "string interpolation". Here is a simple example: x = 2 puts "The variable x is equal to #{x}." --output:-- The variable x is equal to 2. Inside a string you can use the syntax: #{some_var_name} to cause the variable's value to be inserted into the string. > Hmm, I'll mull this over. This is really great information my next > project should go MUCH easier Advice is freely given on the internet. Some of it is bad advice. -- Posted via http://www.ruby-forum.com/.