Problem with block

Why does the following method return nil?

def foo
  info = " What |Removed |Added

···

----------------------------------------------------------------------------
                       Status>RESOLVED |CLOSED
         "
   result = Hash.new
   info.each do |line|
     if line.scan(/\|/)
       field, oldVal, newVal = line.chomp.split(/\s*\|\s*/)
       if (field == 'Status')
         result['status'] = newVal
       end
     end
   end
   result
end

Also, for some reason the statement "if (field == 'Status')" doesn't
return true.

Thanks,

Andy.

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

Andy <ihapaantuh@hotmail.com> writes:

Why does the following method return nil?

def foo
  info = " What |Removed |Added
----------------------------------------------------------------------------
                       Status>RESOLVED |CLOSED
         "
   result = Hash.new
   info.each do |line|
     if line.scan(/\|/)
       field, oldVal, newVal = line.chomp.split(/\s*\|\s*/)
       if (field == 'Status')
         result['status'] = newVal
       end
     end
   end
   result
end

You are not invoking the function "foo", just defining it. Therefore,
none of the code within "foo" gets executed. If you invoke foo after
the final "end" statement, you will see a non-nil result.

Also, for some reason the statement "if (field == 'Status')" doesn't
return true.

The regular expression passed to the line.chomp.split method does not
cause the leading spaces to get stripped out of the string stored in
"field" ... just any trailing spaces that might exist (of which there
are none). Therefore, you should do this:

  if (field.strip == 'Status')

... or something similar.

···

Thanks,

Andy.

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

--
Lloyd Zusman
ljz@asfast.com
God bless you.

Lloyd Zusman wrote:

The regular expression passed to the line.chomp.split method does not
cause the leading spaces to get stripped out of the string stored in
"field" ... just any trailing spaces that might exist (of which there
are none). Therefore, you should do this:

  if (field.strip == 'Status')

... or something similar.

Adding strip to split statement does the trick
'line.chomp.strip.split'

Thanks for the help,

Andy.

···

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