# Problem with block

**URL:** <https://www.ruby-forum.com/t/problem-with-block/59821>\
**Category:** Ruby\
**Created:** [May 7, 2006, 3:49am UTC](https://www.ruby-forum.com/t/problem-with-block/59821 "2006-05-07T03:49:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [May 7, 2006, 3:49am UTC](https://www.ruby-forum.com/t/problem-with-block/59821/1 "2006-05-07T03:49:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [May 7, 2006, 4:20am UTC](https://www.ruby-forum.com/t/problem-with-block/59821/2 "2006-05-07T04:20:12Z")

</div>

Andy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> ```
> 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.

---

<div class="post-metadata">

**Author:** ![andy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@andy](https://www.ruby-forum.com/u/andy)\
**Post date:** [May 7, 2006, 5:03am UTC](https://www.ruby-forum.com/t/problem-with-block/59821/3 "2006-05-07T05:03:15Z")

</div>

Lloyd Z. 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.
