Adam Shelly
5/16/2008 5:05:00 PM
On 5/16/08, Jason Lillywhite <jason.lillywhite@gmail.com> wrote:
> I feel like we need a Forum called, "Ruby Newbie" for people like me. Is
> there one?
>
This one usually works fine.
> 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
>
> ??
>
> 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?**
probably not for a simple test application like that one.
>
> 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?**
>
It's possible that on some systems, the string "Where..?" would stay
in an internal buffer and not get printed. If that happened, your
program would be waiting for input, but the user would have no idea
what it wanted.
If you wanted a robust multi-platform application, a flush would
ensure that the string was printed.
However, on Windows at least, your second version works just fine.
> **I understand chomp to remove record separators from the end, is there
> another reason besides?**
>
Only that one. But it's something you usually want to do.
Imagine your last line was
puts "I live in "+city+", in a nice house."
If you didn't chomp, you'd get a newline right in the middle of your sentence.
Also something like `if (city == 'New York')` would always fail
without the chomp.
> 2. Is there a place I can go to learn more about regular expressions?
I keep going back to "Programming Ruby", but there are probably better choices.
> 3. In IRB, Float::DIG => 15
> **question: This is because float displays to 15 decimals, right?
Float can hold up to 15 significant digits.
irb(main):053:0> f= 1+ 1e-14
=> 1.00000000000001
irb(main):054:0> f= 1+ 1e-15
=> 1.0 #can't hold 16 digits.
irb(main):055:0> f=1+1e14
=> 1.00000000000001e+014
irb(main):056:0> f=1+1e15
=> 1.0e+015
> **question: I see "::" all the time, but don't understand it. What does
> :: mean? Is it showing hierarchy?
It's called the 'scope operator', it lets you Constants (including
other classes) declared inside the definition of the Class on the left
hand side.
If I have:
Class X
Z=0
Class Y;
# ...
end
end
The only way to access Y or Z is with the X:: syntax.
> 4. In the code below:
>
> class NameIt
> def initialize(name)
> @name = name
> end
> def to_s
> puts "My name is: #{@name}"
> end
> end
>
> n1 = NameIt.new("tom")
> n2 = NameIt.new("sally")
>
> n1.to_s
> n2.to_s
>
> **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?
>
Arrays and their iterator methods can be useful:
names = %w{ tom sally douglas} #shorthand for ["tom","sally"...]
nameits = names.map {|name| NameIt.new(name) }
nameits.each{|nit| puts n.to_s }
names.map calls the block for each entry in names, and returns
a new array of the same size, containg the results of the block.
nameits.each simply calls the block once for each entry.
hope this helps,
-Adam