What have you found for these years?

2011-11-29

bad design in gem rest-client

updated 2011-11-29 22:56 pull request sent
require 'webmock'
require 'rest-client'
WebMock::API.stub_request(:get, 'http://host/').to_return(:body => '123')

RestClient.get('http://host/')        # "123"
RestClient.get('http://host/').to_s   # "123"
RestClient.get('http://host/').to_str # "123"
RestClient.get('http://host/').class  # String
ok, here comes the (not) fun part:
RestClient.get('http://host/').to_i         # 200
RestClient.get('http://host/').to_s.to_i    # 200
RestClient.get('http://host/').to_str.to_i  # 200
String(RestClient.get('http://host/')).to_i # 200
Can we remove that offending and surprising to_i?
This works though:
RestClient.get('http://host/').dup.to_i         # 123
String.new(RestClient.get('http://host/')).to_i # 123
"#{RestClient.get('http://host/')}".to_i        # 123
But I don't want to copy the strings all the time!
What if the code is running on a Ruby implementation which
does not support COW (copy-on-write) for strings?
And this didn't work either:
RestClient.get('http://host/').clone.to_i       # 200
Or maybe I should switch to Tony's http?

0 retries:

Post a Comment

Note: Only a member of this blog may post a comment.



All texts are licensed under CC Attribution 3.0