From: Stefano Crocco Date: 2009-07-17T15:21:04+09:00 Subject: Re: trouble with puts On Friday 17 July 2009, Venkat Akkineni wrote: > |Hello > | > | I have been trying to tranlate an application from java to ruby. > |The java program has a main method for testing purposes. I am > |implementing this > |in ruby as a separate file. While doing this I noticed that puts doesn't > |work from inside a method. Let me explain, > | > |case 1: > | > |#just this line in a file called something.rb > | > |puts "something" > | > |>ruby something.rb > |>something > | > |# The same line enclosed in a class > |class Something > | > |puts "something" > | > |end > | > |> ruby something.rb > |> something > | > |# The same line inside a method in a class doesn't work > |class Something > | > |def print_something > | > |puts "something" > | > |end > |end > | > |> ruby something.rb > | > |# I don't see the expected Output > | > | > |Could somebody throw some light on this. > | > |Thanks > |Venkat Are you calling the print_something method? The body of a class (or a top level method call) is executed as soon as it's read, but the body of a method is executed only when the method itself is called. If you want to see the call to puts from print_something, you'll have to call print_something: #file something.rb #here you define the print_something method. What's inside the method #definition isn't executed def print_something puts "something" end #here you call the method you just defined. Now the body of the method is #executed. print_something Then, calling ruby something.rb you should see the output from puts. I hope this helps Stefano