Trouble with puts

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

···

--
Posted via http://www.ruby-forum.com/\.

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

···

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

Code that is within a method definition doesn't get
executed until an instance is created and the method is
called.

Add the following to your third example after the class
definition block for Something:

Something.new.print_something

Gary Wright

···

On Jul 17, 2009, at 2:09 AM, Venkat Akkineni wrote:

# The same line inside a method in a class doesn't work
class Something

def print_something

puts "something"

end

ruby something.rb

# I don't see the expected Output

Could somebody throw some light on this.

Gary Wright wrote:

ruby something.rb

# I don't see the expected Output

Could somebody throw some light on this.

Code that is within a method definition doesn't get
executed until an instance is created and the method is
called.

Add the following to your third example after the class
definition block for Something:

Something.new.print_something

Gary Wright

Thanks for replying Gary and stefano

I have already tried to create an instance only to get the following
error

NameError: uninitialized constant Test

This I assume is thrown because the interpreter cannot find the file. I
ran the irb command from inside the folder containing this file. Doesn't
irb see the file?

Addressing what steffano said, Well I have called the print_something
method from inside the class's constructor. Should I not assume that
when the file is run the class's constructor will be called and hence
the method will be called.

Thanks
Venkat

···

On Jul 17, 2009, at 2:09 AM, Venkat Akkineni wrote:

--
Posted via http://www.ruby-forum.com/\.

>Gary Wright wrote:
>>>> ruby something.rb
>>>
>>> # I don't see the expected Output
>>>
>>>
>>> Could somebody throw some light on this.
>>
>> Code that is within a method definition doesn't get
>> executed until an instance is created and the method is
>> called.
>>
>> Add the following to your third example after the class
>> definition block for Something:
>>
>> Something.new.print_something
>>
>> Gary Wright
>
>Thanks for replying Gary and stefano

First of all, I apologize for not having read your message carefully: I
completely missed the fact that the print_something method was inside a class.
At any rate, most of what I said in my answer is still correct. In the code
you posted, you didn't call the Something#print_something but only defined it.

>This I assume is thrown because the interpreter cannot find the file. I
>ran the irb command from inside the folder containing this file. Doesn't
>irb see the file?

You need to require the file for irb to see it. Try issuing the following
command:

require 'something'

By the way, in the code you posted there's no mention of something called
Test. This means that the code you're running is different from what you
showed us. In this situation, is difficult to for us to help you. Please,
either show us the complete code or run (and report problems with) the code
you posted.

>Addressing what steffano said, Well I have called the print_something
>method from inside the class's constructor. Should I not assume that
>when the file is run the class's constructor will be called and hence
>the method will be called.

No, you didn't (at least in the code you posted). As I said above, you only
defined the method but never actually called it. To do so in a class
constructor, you should have defined an instance method called initialize and
put a call to print_something there:

class Something

  def initialize
    print_something
  end
end

The initialize method is automatically called when you do Something.new and,
in turn, it would call print_something. But you didnt't define an initialize
method and didn't call Something.new.

Stefano

···

On Friday 17 July 2009, Venkat Akkineni wrote:

>> On Jul 17, 2009, at 2:09 AM, Venkat Akkineni wrote:

You'll have to show us all the code then since what
you posted didn't include any reference to Test.

It is important to understand that Ruby doesn't
assume that there is a one-to-one relationship
between a class name (such as Test) and a file
name (such as test.rb). Use 'require' to get
ruby to load and execute a file:

require 'test' # Ruby looks for the file 'test.rb'

Gary Wright

···

On Jul 17, 2009, at 2:37 AM, Venkat Akkineni wrote:

I have already tried to create an instance only to get the following
error

NameError: uninitialized constant Test

Hi Guys

Well to avoid posting the code(for no specific reason), I have posted a
replica of what I was trying to do. Anyhow here is the code.

class Test
  @i
  @x
  @strFile

  def initialize

    #puts "asdfasdfa"
    #puts ARGV[0]

    @x = XmlEngine.new
    @x.configueLog4r(nil)

    testIt

  end

  def testIt

    puts "testIt"

    if ARGV.length < 1
      # XmlEngine.log4rXmlEngine.error()
    end

  end

end

You need to require the file for irb to see it. Try issuing the
following
command:

require 'something'

This actually solved the problem to the extent that the interpreter can
now see the Test.rb class. But while I have your attention let me ask
one more question.

require Test
x = Test.new

Here is the output when I run the above command

C:/Users/venkat/Documents/Ruby Projects/XMLEngine/testing/Test.rb:12:in >`initialize': uninitialized constant Test::XmlEngine (NameError)
from C:/Users/venkat/Documents/Ruby >Projects/XMLEngine/testing/Test.rb:32:in `new'
from C:/Users/venkat/Documents/Ruby >Projects/XMLEngine/testing/Test.rb:32

I have to tell you the project file structure.

XMLEngine -> src -> XmlEngine.rb
          -> testing -> Test.rb

As you can see Test.rb inside testing folder is calling the XmlEngine.rb
inside src folder. Understanding the above error, it is clear that the
interpreter cannot find the XmlEngine.rb. How would I go about showing
Test.rb the location of XmlEngine.rb

Thanks
Venkat
Thanks Venkat

···

--
Posted via http://www.ruby-forum.com/\.

Hi Guys

  Well to avoid posting the code(for no specific reason), I have posted a
replica of what I was trying to do. Anyhow here is the code.

class Test
   @i
   @x
   @strFile

You probably want to initialize those variables:
     @i = nil
     @x = nil
     @strFile = nil

Did you want them to be class variables? As it is, they are instance
variables of the object Test. If you want them to be instance
variables, define them in the initialize method.
    

   def initialize

[...]

     @x = XmlEngine.new

[...]

> XMLEngine -> src -> XmlEngine.rb
> -> testing -> Test.rb

As you can see Test.rb inside testing folder is calling the XmlEngine.rb
inside src folder. Understanding the above error, it is clear that the
interpreter cannot find the XmlEngine.rb. How would I go about showing
Test.rb the location of XmlEngine.rb

You want:

    require '../src/XmlEngine'

···

At 2009-07-17 01:41PM, "Venkat Akkineni" wrote:

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous

Thanks for the reply Glenn

Here's the stuff I have tried and the corresponding output.

Case 1:

require "../src/XmlEngine"

output

C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in >`gem_original_require': no such file to load -- ../src/XmlEngine (LoadError)
       from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in >`require'
       from >C:/Users/venkat/Documents/Projects/RuBravo/XMLEngine/test/XmlEngineTest.rb:4

case 2:

require "C:/Users/venkat/Documents/Ruby Projects/XMLEngine/src/XmlEngine"

output

C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in >`gem_original_require': C:/Users/venkat/Documents/Ruby >Projects/XMLEngine/src/XmlEngine.rb:42: syntax error, unexpected kDO, expecting >kEND (SyntaxError)
do
   ^
       from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in >`require'
       from >C:/Users/venkat/Documents/Projects/RuBravo/XMLEngine/test/XmlEngineTest.rb:4

case 3

Courtesy:
http://opensoul.org/2008/1/9/ruby-s-require-doesn-t-expand-paths

require File.expand_path(File.dirname(__FILE__)).delete("testing") >+ "src/XmlEngine"

output

C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in >`gem_original_require': no such file to load -- >C:/Ur/vka/Docum/Projc/RuBravo/XMLE/src/XmlEngine (LoadError)
       from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in >`require'
       from >C:/Users/venkat/Documents/Projects/RuBravo/XMLEngine/test/XmlEngineTest.rb:4

Help please

···

--
Posted via http://www.ruby-forum.com/\.

So, this one found the XmlEngine.rb file, but that file has a syntax
error (missing "end"). Fix that and you should be OK.

You could also try:
    $: << 'C:/Users/venkat/Documents/Ruby Projects/XMLEngine'
    require 'src/XmlEngine'

where $: is the load path to search for 'require'd files

···

At 2009-07-17 04:08PM, "Venkat Akkineni" wrote:

case 2:

>require "C:/Users/venkat/Documents/Ruby Projects/XMLEngine/src/XmlEngine"

output

>C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in >`gem_original_require': C:/Users/venkat/Documents/Ruby >Projects/XMLEngine/src/XmlEngine.rb:42: syntax error, unexpected kDO, expecting >kEND (SyntaxError)
> do
> ^
> from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in >`require'
> from >C:/Users/venkat/Documents/Projects/RuBravo/XMLEngine/test/XmlEngineTest.rb:4

--
Glenn Jackman
    Write a wise saying and your name will live forever. -- Anonymous