From: Lloyd Zusman Date: 2006-05-07T11:17:30+09:00 Subject: Re: Problem with block Andy 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.