Forum: Ruby Oddities of Ruby to a noob

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
79941ab5294a74685db85edfe0db8b7f?d=identicon&s=25 Hakusa@gmail.com (Guest)
on 2007-05-22 03:55
(Received via mailing list)
I have some good experience in other languages and consider myself a
good programmer, but some of the good techniques I've picked up don't
seem to work here. Here are two examples.

x=aNum
y=0
x.times do
    (y+1).times do
        Stuff
    end
end

y never actually increments. Why? (No pun intended.) y should go up
one x times by the end. Instead it stays at zero. And it's kind of
troubling that is does the loop once even when y is zero; or is that
just because it's a 'do' loop and not a 'for' loop?


And I've learned that to do the gets thing, I have to have
STDOUT.flush before it. It seems that no matter what I do, if it's
even two lines above the gets, I get the gets at the start of the
program even though it was towards the end. Why? If it is such a
common thing, why is STDOUT.flush not automatically called by the
language?


Although as much as this all complexes me, I am very glad to be able
to do this with guilty glee:

STDOUT.flush
puts 'Hello ' + gets.chomp

Although I have a feeling I'll rarely get to use this trick.
83668557add122451315d38b24a9fe62?d=identicon&s=25 Michael Hollins (Guest)
on 2007-05-22 04:11
(Received via mailing list)
Hakusa@gmail.com wrote:
> end
>
> y never actually increments. Why?

because you don't actually increment it anywhere. Perhaps you want
something like:

x=5
y=0
x.times do
     (y += 1).times do
         puts "x = #{x} y = #{y}"
     end
end

cheers,
mick
79941ab5294a74685db85edfe0db8b7f?d=identicon&s=25 Hakusa@gmail.com (Guest)
on 2007-05-22 04:15
(Received via mailing list)
>
> cheers,
> mick

You'd think I'd have gotten that. Oh well. There's one problem fixed.
Thanks.
4feed660d3728526797edeb4f0467384?d=identicon&s=25 Bill Kelly (Guest)
on 2007-05-22 04:17
(Received via mailing list)
From: <Hakusa@gmail.com>
> to do this with guilty glee:
>
> STDOUT.flush
> puts 'Hello ' + gets.chomp
>
> Although I have a feeling I'll rarely get to use this trick.

At the top of your program, try:

STDOUT.sync = true


Hope this helps,

Bill
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2007-05-22 04:24
(Received via mailing list)
On 5/21/07, Hakusa@gmail.com <Hakusa@gmail.com> wrote:
> end
>

That's not how times works,

a_num.times do |x|
   p x
end


> y never actually increments. Why? (No pun intended.) y should go up
> one x times by the end. Instead it stays at zero. And it's kind of
> troubling that is does the loop once even when y is zero; or is that
> just because it's a 'do' loop and not a 'for' loop?
You said do the following y + 1 times.
y is zero, y + 1 is 1. therefore, execute the following block of code,
once.

Other methods you may be interested in are:

0.upto(10) do |y|
   p y
end

or

0.step(5, 1) do |y|
   p y
end

> Although as much as this all complexes me, I am very glad to be able
> to do this with guilty glee:
>
> STDOUT.flush
> puts 'Hello ' + gets.chomp
>
Look what you've written:

puts( 'Hello'  + gets.chomp )

That means it must ask the user for input before it can create the
string to print.
> Although I have a feeling I'll rarely get to use this trick.
>
>
>

I would suggest you look into a ruby tutorial or book such as
Programming Ruby 2nd Ed. or Chris Pine's Learn to Program.
4dea430d31b993abaf41cd9b54f8128d?d=identicon&s=25 Avdi Grimm (avdi)
on 2007-05-22 04:27
(Received via mailing list)
On 5/21/07, Bill Kelly <billk@cts.com> wrote:
> STDOUT.sync = true

But don't make a habit of it.  Synchronous output is a great way to
slow your code down.
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2007-05-22 04:33
(Received via mailing list)
On 5/21/07, Hakusa@gmail.com <Hakusa@gmail.com> wrote:
> end
> program even though it was towards the end. Why? If it is such a
> Although I have a feeling I'll rarely get to use this trick.
>
>
>

I can't answer the STDOUT.flush question because I've never had to use
it, but the lines

> x=aNum
> y=0
> x.times do
>     (y+1).times do
>         Stuff
>     end
> end

doesn't change x or y.  If aNum = 5, for example, the code above would
interpret as:

5.times do
  1.times do
    Stuff
  end
end

You would do Stuff 5 times with x as 5, y as 0 and (y+1) as 1.


If you just want to have a value increment:

aNum.times do |i|
  puts i
end

Note the |i| after do.  This prints

0
1
2
3
4


#times isn't clear that it starts at zero from looking at it, so you
can also write:

0.upto(aNum - 1) do |i|
 puts i
end

Or even

(0..4).each do |i|
 puts i
end


A nested loop:

aNum.times do |i|
  aNum.times do |j|
    puts "i:#{i} j#{j}"
  end
end

Todd
B4c95d150495a8726f8496e96035bdd2?d=identicon&s=25 Just Another Victim of the Ambient Morality (Guest)
on 2007-05-22 04:40
(Received via mailing list)
<Hakusa@gmail.com> wrote in message
news:1179799925.678198.117120@u30g2000hsc.googlegroups.com...
>> end
>>
>> cheers,
>> mick
>
> You'd think I'd have gotten that. Oh well. There's one problem fixed.
> Thanks.

    Just so there are no misunderstandings, do you now understand why:

y + 1

    ...does not increment y?  Does this seem consistent with your
experience
in other programming languages?
    Thank you...
79941ab5294a74685db85edfe0db8b7f?d=identicon&s=25 Hakusa@gmail.com (Guest)
on 2007-05-22 04:41
(Received via mailing list)
> Look what you've written:
>
> puts( 'Hello'  + gets.chomp )
>
> That means it must ask the user for input before it can create the
> string to print.

Exactly! It's awesome! I don't have to make the input and the use of
the word separate. I still need a prompt, but I've cut the middle
step.


> > Although I have a feeling I'll rarely get to use this trick.
>
> I would suggest you look into a ruby tutorial or book such as
> Programming Ruby 2nd Ed. or Chris Pine's Learn to Program.

I'm sure it's a good book, but I'm a zero budget coder at the moment.
No job and only just graduated high school.


> STDOUT.sync = true

What is it anyway? Google's not getting me much good.
4dea430d31b993abaf41cd9b54f8128d?d=identicon&s=25 Avdi Grimm (avdi)
on 2007-05-22 04:47
(Received via mailing list)
On 5/21/07, Hakusa@gmail.com <Hakusa@gmail.com> wrote:
> I'm sure it's a good book, but I'm a zero budget coder at the moment.
> No job and only just graduated high school.

> What is it anyway? Google's not getting me much good.

http://ruby-doc.org/ - scads of documentation, including a few full
books, free and online.
79941ab5294a74685db85edfe0db8b7f?d=identicon&s=25 Hakusa@gmail.com (Guest)
on 2007-05-22 04:55
(Received via mailing list)
On May 21, 10:47 pm, "Avdi Grimm" <a...@avdi.org> wrote:
> Avdi
Awesome. Thanks.


>     Just so there are no misunderstandings, do you now understand why:
>
> y + 1
>
>     ...does not increment y?  Does this seem consistent with your experience
> in other programming languages?
>     Thank you...

I consider that a logical error. I've already rebutted how silly of me
it was to do that and had my laugh about it.
4feed660d3728526797edeb4f0467384?d=identicon&s=25 Bill Kelly (Guest)
on 2007-05-22 04:57
(Received via mailing list)
From: <Hakusa@gmail.com>
>
>> STDOUT.sync = true
>
> What is it anyway? Google's not getting me much good.

Do you have `ri` installed on your system?

$ ri IO#sync=

--------------------------------------------------------------- IO#sync=
     ios.sync = boolean   => boolean
------------------------------------------------------------------------
     Sets the ``sync mode'' to +true+ or +false+. When sync mode is
     true, all output is immediately flushed to the underlying operating
     system and is not buffered internally. Returns the new state. See
     also +IO#fsync+.

        f = File.new("testfile")
        f.sync = true

     _(produces no output)_


As Avdi Grimm pointed out, unnecessarily using unbuffered I/O can
slow down your program.  So if you're worried about that, keep using
flush only when you need it, as you were already doing.

But anyway, setting IO#sync = true is essentially telling the I/O
subsystem to do a flush for you after every write to that file
descriptor.


Regards,

Bill
Ff63c03fd68754adbadd2c6314646bef?d=identicon&s=25 Bill Guindon (agorilla)
on 2007-05-22 05:05
(Received via mailing list)
On 5/21/07, Hakusa@gmail.com <Hakusa@gmail.com> wrote:
> > I would suggest you look into a ruby tutorial or book such as
> > Programming Ruby 2nd Ed. or Chris Pine's Learn to Program.
>
> I'm sure it's a good book, but I'm a zero budget coder at the moment.
> No job and only just graduated high school.

On that note, you can always read the 'student' edition:
http://pine.fm/LearnToProgram/

Once you've made a few bucks, pay him back by buying the paper copy.
http://www.pragmaticprogrammer.com/titles/fr_ltp/
79941ab5294a74685db85edfe0db8b7f?d=identicon&s=25 Hakusa@gmail.com (Guest)
on 2007-05-22 05:50
(Received via mailing list)
Not even out on my own and already developing a list of creditors.
B4c95d150495a8726f8496e96035bdd2?d=identicon&s=25 Just Another Victim of the Ambient Morality (Guest)
on 2007-05-22 06:40
(Received via mailing list)
<Hakusa@gmail.com> wrote in message
news:1179802481.392348.68710@h2g2000hsg.googlegroups.com...
> I consider that a logical error. I've already rebutted how silly of me
> it was to do that and had my laugh about it.


    Yeah, sorry about that.  Your post seemed ambiguous to me and I just
wanted to make sure, for my own piece of mind...
    Thanks...
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (Guest)
on 2007-05-22 07:56
(Received via mailing list)
On Tue, May 22, 2007 at 11:40:06AM +0900, Hakusa@gmail.com wrote:
> > I would suggest you look into a ruby tutorial or book such as
> > Programming Ruby 2nd Ed. or Chris Pine's Learn to Program.
>
> I'm sure it's a good book, but I'm a zero budget coder at the moment.
> No job and only just graduated high school.

The first edition of Programming Ruby is available for free online:
http://www.rubycentral.com/book/

It was written for ruby 1.6, but it still works. You'll just be missing
all
the new good stuff.

The chapter about rubygems from the 2nd edition is also available for
free:
http://www.pragmaticprogrammer.com/titles/ruby/gems.pdf

Regards,

Brian.
05337aed68267daec2996e5ca732d476?d=identicon&s=25 Akbar Home (rinda)
on 2007-05-22 17:15
(Received via mailing list)
On May 22, 9:38 am, "Hak...@gmail.com" <Hak...@gmail.com> wrote:

>
> I'm sure it's a good book, but I'm a zero budget coder at the moment.
> No job and only just graduated high school.
>
>

When I graduated from high school, I only know about clipper
programming language. :( You are much luckier than me.

I find these tutorials all you need to be advanced in ruby
programming.
http://poignantguide.net/ruby/index.html
http://www.sapphiresteel.com/The-Little-Book-Of-Ruby
http://www.troubleshooters.com/codecorn/ruby/basic...

After finishing tutorial, come often to these blogs:
http://www.rubyinside.com/
http://www.rubyfleebie.com/
This topic is locked and can not be replied to.