Issue #14249 has been updated by shevegen (Robert A. Heiler).


As Kentaro showed, there are use cases for [], as-in replacement.

It is quite concise so I think it may be useful for some ruby hackers.

I personally use the more verbose variants usually; as Benoit showed,
".include?" checks, and then often .tr() or .sub() or .gsub(). It
is not as concise as Kentaro's example, but I like working with 
.sub() and .gsub() and .include?() so I tend to use them when possible.

I also use Regexes a lot; they are extremely useful.

Of course in the above, a regex can be used and I think there is more
ruby code that uses regexes instead:

    s[/age/] = "heese"

I don't know offhand about differences as hanmac stated, but I personally
use regexes almost always, so the // syntax. But Yuki also mentioned a
difference, e. g. the String having to become a regex (and I remember
in the old pickaxe, that creating a regex is more costly than creating
a String).

Since some time we can use regexes for .start_with?() and .end_with?()
checks; before that, I think we could use only Strings.

In .include?() checks, we seem to be only able to use Strings. Would
adding regex support there be good? I don't want to distract from the
issue here, so I am just mentioning it.

Anyway, Ana wrote:

> the question is if it makes sense to keep str[match_str]. It is a
> confusing method...

I don't really think it is a confusing method - it seems very simple
to me. I don't use it myself because of alternatives, but I think 
trying to want to make ruby "perfect" in every theoretical aspect,
is very, very difficult, even without backwards compatibility.

Ruby is a great language but that does not necessarily mean that 100%
of ruby is perfect (or great). Ruby hackers have to decide on what
they want to write, how to write it and so forth.

What works in my case is to focus (and use) what I like in ruby and
mostly ignore the rest. (I am not speaking about string[match_string]
but e. g. @@class_variables as example; I realized one day that I do
not need it, so I stopped using them and everything is fine).

Perhaps that also works for your use cases when writing ruby code Ana.

----------------------------------------
Feature #14249: Remove str[match_str]
https://bugs.ruby-lang.org/issues/14249#change-72184

* Author: ana06 (Ana Maria Martinez Gomez)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I wonder if `str[match_str]` makes sense.

``` Ruby
"ana"['a'] => "a"

```

I would say this is not expected and it brings problems, for example when accessing nested hashes. For example:

``` Ruby
params = { "user" => "Nicolas Cage" } => {"user"=>"Nicolas Cage"}

params["user"]["age"] => "age"

```

I think `str[regexp]` is enough and that `str[match_str]` can be removed.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>