Alias String#gsub to replace or deprecate gsub

similar https://github.com/crystal-lang/crystal/pull/8452

python use replace

java use replace

c++ use replace

c# use replace

javascript use replace

go use replace

swift use replace

rust use replace

even php use replace

1 Like

this is a complete list

ruby and lua also use gsub, I prefer replace

ruby has replace method, but not for finding replace.

I agree with you. Ruby uses a lot of unixisms and we just blindly copied them.

That said, it should replace gsub before 1.0. And what name to use instead of sub?

2 Likes

a replace parameter like count or replace_all for gsub and replace for sub,

or replace for gsub and replace_once for sub

I disagree.

This is creating a breaking change on a mere opinion, many other languages preferring a different name is a quite weak objective argument, IMO. Secondly Crystal still has a strong heritage from Ruby, why would we need to deny it. Also I don’t follow how this is a “Unixism” and why that would be bad.

On the contrary, in languages using replace I’m always confused whether it’s a global or a single substitution and have to look it up everytime. sub/gsub have this nicely in their name.

6 Likes

gsub is not friendly for a beginner.

beginer will pick replace first when they need this functionality.

ruby want to do this, but there is a replace method. this will break backward compatibility.

Although gsub is a unixisms, it’s a also a good short name for the operation. The case of Enumerable#grep was more an abuse of the semantic of the operation and extended behaviour.

Although there is benefit in more languages having the same operation names, in this case I don’t think the name is wrong.

I agree with @jhass here.

I think is too late for this change in 1.0, the next release is 1.0-pre1 and there is no deprecation cycle to go through. We are doing some breaking-changes but not in methods that are probably so widespred used like String#gsub.

I would not mind having aliases for replace and replace_all, but we are almost alias free.

For curiosity, can you share a link for this?

2 Likes

Whenever I’m pairing with someone on Ruby and I use gsub I wonder whether they know what that does. gsub is not an English word, it’s cryptic.

Why is it late to change this? If you get an error for this it’s very easy to change, you just gsub("gsub","replace") (just kidding about the name I just used).

When I started coding in Ruby and saw gsub I had no idea what that was. I had to look up docs. I then I had to remember that name. Seeing sub was also cryptic and I had to learn that as well. If it was replace then I wouldn’t have any doubts about it.

Crystal should be beginner friendly, not hardcore friendly. So if a change is still possible, I’d like us to make it.

And we should review all method names in the stdlib before 1.0 too…

5 Likes

no link, but ruby promise not breaking backward compatibility, and now the replace method in ruby is for another use.

sub is “substitution”, means “replace” too, but a bit strange.

1 Like

Yes, but “sub” is “sub”, it’s not “substitute”. It’s like a method named “rep” because it replaces things.

Same with me. Even I use Ruby ( on Rails) since 2005, I always forget about gsub if I am not use Ruby for a while.

I would apreciate it if gsub was changed to replace or if replace was an alias for gsub.

I always forget about gsub,
so I start with .replace, go to google or the docs and eventually find gsub.

1 Like

I agree 100%. It makes no sense to make this kind of breaking change simply because some other languages use replace. Not to mention, one of the selling points of Crystal, for me at least, is the near identical syntax to Ruby. It’s pretty straightforward. I always understood it as gsub = global substitute.

It’s one small method to remember for those not used to it. A common one too. Not worth breaking changes.

2 Likes

if someone do not like this, we can do alias, crystal alias has no run time overhead. and not affected on binary size.

There’s no alias in Crystal in we try to avoid aliases. Aliases have cognitive load: now everyone needs io know both names. It also affects compilation time because LLVM will have to process that to possibly online the redirected call. And it’s not clear inline will happen so it can effectively affect binary size.

3 Likes