Block does not return correct value vs variable assign

I have this :
def profile
page = @agent.get(@uri)
MultiJson.decode(page.body.gsub(/([a-zA-Z]+):/, ‘"\1":’)) do |file|
if file[‘errCode’] == 0
return file[‘miniprofile’]
else
return
{‘errCode’=>file[‘errCode’],‘errText’=>file[‘errText’],‘uri’=>page.uri}
end
end
end

This always returns the MultiJson output. (not what I want) It seems as
if it doesn’t even
do the block. If I use a variable like this :

def profile
page = @agent.get(@uri)
file = MultiJson.decode(page.body.gsub(/([a-zA-Z]+):/, ‘"\1":’))
if file[‘errCode’] == 0
return file[‘miniprofile’]
else
return
{‘errCode’=>file[‘errCode’],‘errText’=>file[‘errText’],‘uri’=>page.uri}
end
end

It returns one or the other inside the if statements.

Shouldn’t both of these return the same thing

On Wed, Oct 5, 2011 at 9:27 PM, Kassym D. [email protected]
wrote:

end
return file[‘miniprofile’]
else
return
{‘errCode’=>file[‘errCode’],‘errText’=>file[‘errText’],‘uri’=>page.uri}
end
end

It returns one or the other inside the if statements.

Shouldn’t both of these return the same thing

It depends on what MultiJson.decode does with the block that you are
passing to it.
Can you show us that code?
It might be that the decode method is just not doing anything with the
block (doesn’t yield or call it), that could explain the behaviour you
are seeing.

Jesus.

I’m using the MultiJson gem.
https://github.com/intridea/multi_json

Would you suggest to use another JSON parser ? If so, suggestions ?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs