Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias #string to #to_s/#to_str #103

Closed
wants to merge 3 commits into from
Closed

Alias #string to #to_s/#to_str #103

wants to merge 3 commits into from

Conversation

sebyx07
Copy link

@sebyx07 sebyx07 commented Aug 22, 2024

fix GH-102

imo, a lot of users would rather call buffer.to_s instead of the .string method, nobody atm depends on the .to_s. There already is .inspect that behaves like the current to_s

bonus, having .to_s, now you can interpolate string io into strings w/o calling .to_s

@sebyx07 sebyx07 changed the title Alias string .to_s Alias #string to #to_s/#to_str Aug 28, 2024
@nobu
Copy link
Member

nobu commented Aug 30, 2024

StringIO is an IO-like class but not a String-compatible class, so StringIO#to_str is not acceptable.

@sebyx07
Copy link
Author

sebyx07 commented Sep 2, 2024

@nobu - but #to_s is fine?

Comment on lines +1035 to +1060
def test_to_s_with_interpolation
content = "Hello, StringIO!"
stringio = StringIO.new(content)

assert_equal "Content: #{content}", "Content: #{stringio}"

assert_equal "Before #{content} After", "Before #{stringio} After"

empty_stringio = StringIO.new
assert_equal "Empty: ", "Empty: #{empty_stringio}"

utf16_content = content.encode("UTF-16LE")
stringio_with_encoding = StringIO.new(utf16_content)
assert_equal "UTF-16LE: #{utf16_content.encode('UTF-8')}", "UTF-16LE: #{stringio_with_encoding.to_s.encode('UTF-8')}"

assert_equal "UTF-16LE", stringio_with_encoding.to_s.encoding.name

stringio.pos = 7
assert_equal "Moved: #{content}", "Moved: #{stringio}"

stringio.close
assert_equal "Closed: #{content}", "Closed: #{stringio}"

assert_equal "Start #{content} Middle #{empty_stringio} End", "Start #{stringio} Middle #{empty_stringio} End"
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interpolation is not a feature of StringIO.
This does not make sense here.

@nobu nobu closed this Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Alias .string to .to_s
3 participants