From: Todd Benson Date: 2008-05-17T01:37:25+09:00 Subject: Re: newbie questions On Fri, May 16, 2008 at 10:52 AM, Jason Lillywhite wrote: > I feel like we need a Forum called, "Ruby Newbie" for people like me. Is > there one? > > I am hoping someone could help me with the following (probably) simple > questions (my nested questions are surrounded by **): > > 1. Someone said to use the following code for getting a string and > printing to the screen: > > puts "Where do you live?" > STDOUT.flush > city = gets.chomp > puts "I live in " + city > > **why not just do this:** > > puts "Where do you live?" > city = gets() > puts "I live in " + city or... puts "Where do you live?" puts "You live in " << gets > > ?? > > regarding STDOUT: the comment was made - STDOUT is a global constant > which is the actual standard output stream for the program. > > **but is it necessary?** > > A comment was made: flush flushes any buffered data within io to the > underlying operating system (note that this is Ruby internal buffering > only; the OS may buffer the data as well). The usage is not mandatory > but recommended. > > **I still don't understand why flush is recommended. Is my omission > going to create a problem?** > > **I understand chomp to remove record separators from the end, is there > another reason besides?** > > 2. Is there a place I can go to learn more about regular expressions? I've occasionally found www.regexp.info useful. > 3. In IRB, Float::DIG => 15 > **question: This is because float displays to 15 decimals, right? > **question: I see "::" all the time, but don't understand it. What does > :: mean? Is it showing hierarchy? It's the scope operator. Page 330 of the Pickaxe, 2nd Ed. > 4. In the code below: > > class NameIt > def initialize(name) > @name = name > end > def to_s > puts "My name is: #{@name}" > end def to_s "My name is: " << name # no puts end > end > > n1 = NameIt.new("tom") > n2 = NameIt.new("sally") > > n1.to_s > n2.to_s puts n1 puts n2 > > **the last 4 lines seem very repetative. this is something I've wondered > for a long time. Is there a way to compact the last 4 lines if you are > trying to list a whole bunch of names at once using the method to_s? > > thank very much! > > Jason Todd