Crystal: String#tr with empty String

Created on 9 Aug 2016  路  6Comments  路  Source: crystal-lang/crystal

Hello!

"aabbcc".tr("b", "")

Output:

Index out of bounds (IndexError)
[4311543] *CallStack::unwind:Array(Pointer(Void)) +87
[4311434] *CallStack#initialize:Array(Pointer(Void)) +10
[4311386] *CallStack::new:CallStack +42
[4295912] *raise<IndexError>:NoReturn +24
[4397942] *Char::Reader#next_char:Char +38
[4338141] *String#tr<String, String>:String +14253
[4305298] *__icr_exec__:String +130
[4292040] ???
[4304873] main +41
[140625133621969] __libc_start_main +241
[4290714] _start +42
[0] ???

In Ruby:

"aacc"

It's not cool :(

bug stdlib

All 6 comments

It should at least give a better error message, but I'm actually not sure about special case. You can just use String#delete for it.

@jhass Okay, thanks, I will use String#delete now.

@paddor I'm not understanding why throwing an ArgumentError is the correct solution to this. What's wrong with replacing something with an empty String from a conceptual / usability standpoint? I think this solution violates the Principle of Least Surprise. I would absolutely expect it to work like @AlexWayfer did.

I like that if the exception is going to be thrown it says "to can't be empty" and that does help, but still...

also, if this issue is no longer up for debate because of the "don't crash if replacement is empty" commit, it should be closed. If it _is_ up for debate though, let's debate! ;)

@masukomi Maybe you wanna read up on the discussion on #3129.

3129 is merged, behavior is expected, issue can be closed?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pbrusco picture pbrusco  路  3Comments

jhass picture jhass  路  3Comments

ArthurZ picture ArthurZ  路  3Comments

lgphp picture lgphp  路  3Comments

RX14 picture RX14  路  3Comments