#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    USA
    Posts
    42
    Rep Power
    7

    Sequence of execution?


    Code:
    class Test
      attr_accessor :value
    
      def initialize(value=nil)
        @value = value
      end
    
      def ask(prompt)
        puts prompt + " "
        answer = gets
        answer ? answer.chomp : nil
      end
      
    end
    
    if __FILE__ == $0
      puts "Welcome message!"
      t1 = Test.new("What is your name")
      var = t1.ask "#{t1.value}"
      puts "#{var}"
    end
    Why does it start waiting for user input straight away?

    Isn't the Welcome message statement first?

    Please guide
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Location
    New Jersey
    Posts
    219
    Rep Power
    137
    You're right—it should print out "Welcome message!" and "What is your name" before waiting for input. It might be the software you are using to run the program. Try running it from the command line.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    USA
    Posts
    42
    Rep Power
    7
    I flushed the STDOUT, that works. Actually i made this mistake once before too. So, I remembered. But, anyway thx for your reply
    Last edited by m4st3rm1nd; November 18th, 2008 at 11:33 PM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    Originally Posted by m4st3rm1nd
    I flushed the STDOUT, that works. Actually i made this mistake once before too. So, I remembered. But, anyway thx for your reply
    Yes, the standard output stream is generally buffered. If you want unbuffered you can either flush the buffer after your writes or write to the standard error stream.
    You may want to keep in mind that this is not Ruby specific.
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog

IMN logo majestic logo threadwatch logo seochat tools logo