beginner Q: Kernel#puts, STDOUT, $stdout relation

Discussion in 'Ruby' started by Andreas S, Dec 8, 2006.

  1. Andreas S

    Andreas S Guest

    Can anybody help me understanding the relation between Kernel#puts, STDOUT
    and $stdout?

    I thought 'puts' is equivalent to '$stdout.puts'. Why 'puts' doesn't give
    same result as 'STDOUT.puts'?

    class << STDOUT
    def puts(*args)
    args[0] = "I say " + args[0] unless args.empty?
    super(args)
    end
    end

    puts "hello"
    STDOUT.puts "hello"
    $stdout.puts "hello"
    => hello
    I say hello
    I say hello


    Thank you in advance
    -andre

    _________________________________________________________________
    Talk now to your Hotmail contacts with Windows Live Messenger.
    http://clk.atdmt.com/MSN/go/msnnkwm.../?href=http://get.live.com/messenger/overview
     
    Andreas S, Dec 8, 2006
    #1
    1. Advertising

  2. Puts is a method of Kernel.

    Andreas S wrote:
    > Can anybody help me understanding the relation between Kernel#puts,
    > STDOUT and $stdout?
    >
    > I thought 'puts' is equivalent to '$stdout.puts'. Why 'puts' doesn't
    > give same result as 'STDOUT.puts'?
    >
    > class << STDOUT
    > def puts(*args)
    > args[0] = "I say " + args[0] unless args.empty?
    > super(args)
    > end
    > end
    >
    > puts "hello"
    > STDOUT.puts "hello"
    > $stdout.puts "hello"
    > => hello
    > I say hello
    > I say hello
    >
    >
    > Thank you in advance
    > -andre
    >
    > _________________________________________________________________
    > Talk now to your Hotmail contacts with Windows Live Messenger.
    > http://clk.atdmt.com/MSN/go/msnnkwm.../?href=http://get.live.com/messenger/overview
    >
    >
    >
    >
     
    Daniel Finnie, Dec 8, 2006
    #2
    1. Advertising

  3. Andreas S

    Jacob Fugal Guest

    On 12/8/06, Andreas S <> wrote:
    > >From: Daniel Finnie <>
    > >Reply-To:
    > >To: (ruby-talk ML)
    > >Subject: Re: beginner Q: Kernel#puts, STDOUT, $stdout relation
    > >Date: Sat, 9 Dec 2006 07:28:20 +0900
    > >
    > >Puts is a method of Kernel.

    >
    > But, doesn't it depend on what IO object $stdout holds? I redefined $stdout
    > 'puts' function, but why it does not affect Kernel's puts method?


    It's a tricky relationship, and I'm not quite sure how or why it works
    this way, but Kernel#puts does not use STDOUT's (or $stdout, they can
    be different) puts. It does use STDOUT/$stdout (I'm not really sure
    which) but IIRC Kernel#puts is implemented directly using the write
    method. Caveat here though -- the write method is called *twice*, once
    for the argument, then again for the newline. So modifying your source
    to rewrite "write" instead of "puts":

    class << STDOUT
    def write(*args)
    args[0] = "I say " + args[0] unless args.empty?
    super(args)
    end
    end

    puts "hello"
    STDOUT.puts "hello"
    $stdout.puts "hello"

    produces:

    $ ruby test.rb
    I say helloI say
    I say helloI say
    I say helloI say

    Jacob Fugal
     
    Jacob Fugal, Dec 9, 2006
    #3
  4. Andreas S

    Eric Hodel Guest

    On Dec 8, 2006, at 14:16 , Andreas S wrote:
    > Can anybody help me understanding the relation between Kernel#puts,
    > STDOUT and $stdout?
    >
    > I thought 'puts' is equivalent to '$stdout.puts'. Why 'puts'
    > doesn't give same result as 'STDOUT.puts'?
    >
    > class << STDOUT
    > def puts(*args)
    > args[0] = "I say " + args[0] unless args.empty?
    > super(args)
    > end
    > end
    >
    > puts "hello"


    This calls $stdout.write "hello\n"

    > STDOUT.puts "hello"
    > $stdout.puts "hello"
    > => hello
    > I say hello
    > I say hello


    PS:

    Please mess with $stdout, not STDOUT, if at all possible.

    http://blog.segment7.net/articles/2006/08/17/stdout-vs-stdout

    --
    Eric Hodel - - http://blog.segment7.net

    I LIT YOUR GEM ON FIRE!
     
    Eric Hodel, Dec 9, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. yogesh
    Replies:
    3
    Views:
    610
    Kenny McCormack
    Feb 12, 2006
  2. Replies:
    3
    Views:
    187
  3. Michael Brooks
    Replies:
    22
    Views:
    435
    Rick DeNatale
    Mar 27, 2007
  4. Raj Singh
    Replies:
    4
    Views:
    155
    Ryan Ingram
    Jan 29, 2008
  5. Lars Gierth
    Replies:
    6
    Views:
    244
    David Masover
    Mar 20, 2010
Loading...

Share This Page