ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:92732] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range
       [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
@ 2019-05-21  1:46 ` manga.osyo
  2019-05-21  8:16 ` [ruby-core:92738] " shevegen
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: manga.osyo @ 2019-05-21  1:46 UTC (permalink / raw)
  To: ruby-core

Issue #15864 has been reported by osyo (manga osyo).

----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:92738] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range
       [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
  2019-05-21  1:46 ` [ruby-core:92732] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range manga.osyo
@ 2019-05-21  8:16 ` shevegen
  2019-05-21 18:58 ` [ruby-core:92748] " eregontp
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: shevegen @ 2019-05-21  8:16 UTC (permalink / raw)
  To: ruby-core

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


I think the proposal makes sense. The method names are a bit odd though.

I have no huge qualms with the inf* related methods, such as infinite? or
finite? although it reads a bit odd; but I think that .beginless? and
.endless? are very clumsy names.

Unfortunately I do not have better alternative suggestions either.

Perhaps it could suffice to only add e. g. ".infinite?" and we may not
even need ".finite?".

The use case on the other hand is clear to me, IMO, in particular 
"(1..).size return Infinity, but ("a"..).size return nil." so it
would make sense to give ruby users the ability to check for 
infinite in advance, rather than check for nil lateron.

> I think that there is no relation between Float::INFINITY and infinite Range

I guess a/any concept of infinity should not necessarily be intrinsicalled
tied down to a particular constant (e. g. Float::INFINITY) per se; in the latter
case it would seem more like an implementation detail (to me), rather than a
more general concept for/of infinity.

----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864#change-78100

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:92748] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range
       [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
  2019-05-21  1:46 ` [ruby-core:92732] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range manga.osyo
  2019-05-21  8:16 ` [ruby-core:92738] " shevegen
@ 2019-05-21 18:58 ` eregontp
  2019-05-21 23:13 ` [ruby-core:92750] " mame
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: eregontp @ 2019-05-21 18:58 UTC (permalink / raw)
  To: ruby-core

Issue #15864 has been updated by Eregon (Benoit Daloze).


Why is `range.begin.nil? || range.end.nil?` not enough to check if beginless or endless Range?

Maybe we should integrate beginless/endless in pattern matching?

The use-case can be simplified quite a bit if we can assume `false` is not used as a Range endpoint:

```ruby
def search_in(range)
  query = "/search"
  if range.begin && range.end
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.end
    "#{query}?to=#{range.end}"
  elsif range.begin
    "#{query}?from=#{range.begin}"
  else
    query
  end
end
```

I think `infinite?` is a different concept, which already exists as Numeric#infinite?, and should definitely handle the Float::INFINITY case if introduced.

I think these are probably bugs:
```ruby
p ("a"..).size      # => nil
p (.."z").size      # => nil
```
and should return infinity.

----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864#change-78110

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:92750] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range
       [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-05-21 18:58 ` [ruby-core:92748] " eregontp
@ 2019-05-21 23:13 ` mame
  2019-05-22  1:14 ` [ruby-core:92751] " manga.osyo
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: mame @ 2019-05-21 23:13 UTC (permalink / raw)
  To: ruby-core

Issue #15864 has been updated by mame (Yusuke Endoh).


I have no strong opinion to the proposal itself, but I'm also skeptical to the motivating example.  I'd like to write:

```ruby
def search_in(range)
  path = "/search"
  query = []
  query << "from=#{ range.begin }" if range.begin
  query << "to="#{ range.end }" if range.end
  query.empty? ? path : path + "?" + query.join("&")
end
```

I have no idea when we want to check if a range is just infinite without checking the actual values.

Currently, `("a"..).size #=> nil` is intended because `("a".."z").size #=> nil`.  According to @marcandre https://bugs.ruby-lang.org/issues/14699#change-71575, there is no special reason why `("a".."z").size` returns nil.  So after `("a".."z").size` is implemented correctly, I agree that `("a"..).size` should return infinity.

----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864#change-78112

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:92751] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range
       [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
                   ` (3 preceding siblings ...)
  2019-05-21 23:13 ` [ruby-core:92750] " mame
@ 2019-05-22  1:14 ` manga.osyo
  2019-05-22  1:22 ` [ruby-core:92752] " manga.osyo
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: manga.osyo @ 2019-05-22  1:14 UTC (permalink / raw)
  To: ruby-core

Issue #15864 has been updated by osyo (manga osyo).


> I think infinite? is a different concept, which already exists as Numeric#infinite?, and should definitely handle the Float::INFINITY case if introduced

I also considered the following implementation.

```ruby
class Range
  def beginless?
    self.begin.nil? || (self.begin.respond_to?(:infinite?) && self.begin.infinite?)
  end
end
```

MEMO: `Numeric#infinite?` should also consider returning something other than `true / false`.


> So after ("a".."z").size is implemented correctly, I agree that ("a"..).size should return infinity.

I'm concerned about the performance of `("a".."z").size`.
Can implement `("a".."z").size` with` O (1)` ?


----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864#change-78113

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:92752] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range
       [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
                   ` (4 preceding siblings ...)
  2019-05-22  1:14 ` [ruby-core:92751] " manga.osyo
@ 2019-05-22  1:22 ` manga.osyo
  2019-05-22  2:54 ` [ruby-core:92754] " mame
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: manga.osyo @ 2019-05-22  1:22 UTC (permalink / raw)
  To: ruby-core

Issue #15864 has been updated by osyo (manga osyo).


How about doing this?

```ruby
("a".."z").size # => nil
(.."z").size    # => Infinity
("a"..).size    # => Infinity
```


----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864#change-78114

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:92754] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range
       [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
                   ` (5 preceding siblings ...)
  2019-05-22  1:22 ` [ruby-core:92752] " manga.osyo
@ 2019-05-22  2:54 ` mame
  2019-05-22  8:56 ` [ruby-core:92776] " matz
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: mame @ 2019-05-22  2:54 UTC (permalink / raw)
  To: ruby-core

Issue #15864 has been updated by mame (Yusuke Endoh).


Do you mean that `Range#infinite?` is no longer needed if the behavior of `("a"..).size` is changed?  If not, I think it is a different topic from this ticket.

----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864#change-78117

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:92776] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range
       [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
                   ` (6 preceding siblings ...)
  2019-05-22  2:54 ` [ruby-core:92754] " mame
@ 2019-05-22  8:56 ` matz
  2019-08-22  0:07 ` [ruby-core:94468] [Ruby master " muraken
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 11+ messages in thread
From: matz @ 2019-05-22  8:56 UTC (permalink / raw)
  To: ruby-core

Issue #15864 has been updated by matz (Yukihiro Matsumoto).


I am against having `finite?` or `infinite?` methods. I don't think there's no use-case for those methods.
Meanwhile, I see the small possibility for the usage of `beginless?` and `endless?` methods. But I don't like the names.

Matz.


----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864#change-78141

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:94468] [Ruby master Feature#15864] Proposal: Add methods to determine if it is an infinite range
       [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
                   ` (7 preceding siblings ...)
  2019-05-22  8:56 ` [ruby-core:92776] " matz
@ 2019-08-22  0:07 ` muraken
  2019-08-22  3:23 ` [ruby-core:94471] " sawadatsuyoshi
  2019-08-22  7:36 ` [ruby-core:94475] " mame
  10 siblings, 0 replies; 11+ messages in thread
From: muraken @ 2019-08-22  0:07 UTC (permalink / raw)
  To: ruby-core

Issue #15864 has been updated by mrkn (Kenta Murata).


Other candidates from mathematical terms:

- `unbounded?` for checking whether `begin` or `end` is `nil`
- `lower_unbounded?`, `left_unbounded?`, or `unbounded_below?` for checking whether `begin` is `nil`
- `upper_unbounded?`, `right_unbounded?`, or `unbounded_above?` for checking whether `end` is `nil`

----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864#change-80903

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:94471] [Ruby master Feature#15864] Proposal: Add methods to determine if it is an infinite range
       [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
                   ` (8 preceding siblings ...)
  2019-08-22  0:07 ` [ruby-core:94468] [Ruby master " muraken
@ 2019-08-22  3:23 ` sawadatsuyoshi
  2019-08-22  7:36 ` [ruby-core:94475] " mame
  10 siblings, 0 replies; 11+ messages in thread
From: sawadatsuyoshi @ 2019-08-22  3:23 UTC (permalink / raw)
  To: ruby-core

Issue #15864 has been updated by sawa (Tsuyoshi Sawada).


Having negative meaning in the method name would make it complicated. Having `begin?` and `end?` instead of `beginless?` and `endless?` would be more natural, and easy to comprehend.

In fact, the given use case:

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end
```

is not straightforward; it is twisted. The intent of the `elsif range.beginless?` condition is actually to check whether the end exists, not whether the beginning is absent. Using `begin?` and `end?`, a more straightforward code can be written as follows (I believe there is no case that corresponds to your `else` case).


```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.end?
    "#{query}?to=#{range.end}"
  elsif range.begin?
    "#{query}?from=#{range.begin}"
  end
end
```


----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864#change-80906

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




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

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:94475] [Ruby master Feature#15864] Proposal: Add methods to determine if it is an infinite range
       [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
                   ` (9 preceding siblings ...)
  2019-08-22  3:23 ` [ruby-core:94471] " sawadatsuyoshi
@ 2019-08-22  7:36 ` mame
  10 siblings, 0 replies; 11+ messages in thread
From: mame @ 2019-08-22  7:36 UTC (permalink / raw)
  To: ruby-core

Issue #15864 has been updated by mame (Yusuke Endoh).


> Having `begin?` and `end?` instead of `beginless?` and `endless?` would be more natural, and easy to comprehend.

Agreed.  Moreover, `Range#begin` and `#end` just work.


----------------------------------------
Feature #15864: Proposal: Add methods to determine if it is an infinite range
https://bugs.ruby-lang.org/issues/15864#change-80910

* Author: osyo (manga osyo)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------

## Summary

Proposal to add methods to determine if it is an infinite range.


## Current status

```ruby
# begin / end return nil
p (..10).begin  # => nil
p (1..).end     # => nil

# But, first / last raise an exception
p (..10).first  # Error: in `first': cannot get the first element of beginless range (RangeError)
p (1..).last    # Error: in `last': cannot get the last element of endless range (RangeError)

# Returns Infinity if it is a Numeric
p (1..10).size      # => 10
p (1..).size        # => Infinity
p (..1).size        # => Infinity

# Otherwise returns nil
p ("a".."z").size   # => nil
p ("a"..).size      # => nil
p (.."z").size      # => nil
```

* `(..10).begin` return `nil`, but `(..10).first` raise an exception.
* `(1..).size` return `Infinity`, but `("a"..).size` return `nil`.
* Behavior changes depending on the state of Range and the called method.
* It is difficult to determine if it is an infinite range.


## Proposal methods

* `Range#beginless?`
  * return `true` if `begin` is `nil`
* `Range#endless?`
  * return `true` if `end` is `nil`
* `Range#infinite?`
  * return `true` if `begin` is `nil` or `end` is `nil`
* `Range#finite?`
  * return `true` if `begin` is not `nil` and `end` is not `nil`


## Example

```ruby
p (1..10).beginless?  # => false
p (1..10).endless?    # => false
p (1..10).infinite?   # => false
p (1..10).finite?     # => true

p (..10).beginless?   # => true
p (..10).endless?     # => false
p (..10).infinite?    # => true
p (..10).finite?      # => false

p (1..).beginless?    # => false
p (1..).endless?      # => true
p (1..).infinite?     # => true
p (1..).finite?       # => false

# NOTE: Float::INFINITY is not support
p (1..Float::INFINITY).beginless?    # => false
p (1..Float::INFINITY).endless?      # => false
p (1..Float::INFINITY).infinite?     # => false
p (1..Float::INFINITY).finite?       # => true
```


## Use case

### before

```ruby
def search_in(range)
  query = "/search"
  if !(range.begin.nil? || range.end.nil?)
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.begin.nil?
    "#{query}?to=#{range.end}"
  elsif range.end.nil?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```

### after

```ruby
def search_in(range)
  query = "/search"
  if range.finite?
    "#{query}?from=#{range.begin}&to=#{range.end}"
  elsif range.beginless?
    "#{query}?to=#{range.end}"
  elsif range.endless?
    "#{query}?from=#{range.begin}"
  else
    query
  end
end

p search_in("2019-1-1".."2019-4-30")
# => "/search?from=2019-1-1&to=2019-4-30"

p search_in(.."2019-4-30")
# => "/search?to=2019-4-30"

p search_in("2019-5-1"..)
# => "/search?from=2019-5-1"
```


## Memo

* Check whether the tip is infinite only with `nil`.
* `Float::INFINITY` is not supported.
  * I think that there is no relation between `Float::INFINITY` and infinite Range.
* I would like an opinion on how to determine if it is infinite.
  * see `range.begin.infinite?` ?
* Uhether there is a better name for the method name.
  * e.g. `#infinite?` to `#infinity?`.


Thank you.

github pull request: https://github.com/ruby/ruby/pull/2196




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

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2019-08-22  7:36 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-15864.20190521014637@ruby-lang.org>
2019-05-21  1:46 ` [ruby-core:92732] [Ruby trunk Feature#15864] Proposal: Add methods to determine if it is an infinite range manga.osyo
2019-05-21  8:16 ` [ruby-core:92738] " shevegen
2019-05-21 18:58 ` [ruby-core:92748] " eregontp
2019-05-21 23:13 ` [ruby-core:92750] " mame
2019-05-22  1:14 ` [ruby-core:92751] " manga.osyo
2019-05-22  1:22 ` [ruby-core:92752] " manga.osyo
2019-05-22  2:54 ` [ruby-core:92754] " mame
2019-05-22  8:56 ` [ruby-core:92776] " matz
2019-08-22  0:07 ` [ruby-core:94468] [Ruby master " muraken
2019-08-22  3:23 ` [ruby-core:94471] " sawadatsuyoshi
2019-08-22  7:36 ` [ruby-core:94475] " mame

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).