[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ranges and Enumerable problems

Glen Holcomb

7/2/2008 2:49:00 PM

[Note: parts of this message were removed to make it a legal post.]

Okay so when I play with "!".."~" wrong things happen.

("!".."~").include?("W")
true

("!.."~").include?(" ")
false

so far so good, however.

("!".."~").each do |c|
puts c
end

!
"
#
$
%
&
'
(
)
*
+
,
-
23 Answers

Robert Klemme

7/2/2008 3:13:00 PM

0

2008/7/2 Glen Holcomb <damnbigman@gmail.com>:
> Okay so when I play with "!".."~" wrong things happen.
>
> ("!".."~").include?("W")
> true
>
> ("!.."~").include?(" ")
> false
>
> so far so good, however.
>
> ("!".."~").each do |c|
> puts c
> end
>
> !
> "
> #
> $
> %
> &
> '
> (
> )
> *
> +
> ,
> -
> .
> /
> 0
> 1
> 2
> 3
> 4
> 5
> 6
> 7
> 8
> 9
> => "!".."~"
>
> This is wrong!
>
> ("!".."~").last
> => "~"
>
> This is also right.
>
> It looks to me like a problem with the Enumerable mixin but I guess the
> question is why?

Note this

irb(main):003:0> "9".succ
=> "10"

Try this instead:

(?!..?~).each {|c| p c.chr}

irb(main):008:0> (?!..?~).include? ?=> false


Kind regards

robert



--
use.inject do |as, often| as.you_can - without end

Glen Holcomb

7/2/2008 4:30:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Wed, Jul 2, 2008 at 9:12 AM, Robert Klemme <shortcutter@googlemail.com>
wrote:

> 2008/7/2 Glen Holcomb <damnbigman@gmail.com>:
> > Okay so when I play with "!".."~" wrong things happen.
> >
> > ("!".."~").include?("W")
> > true
> >
> > ("!.."~").include?(" ")
> > false
> >
> > so far so good, however.
> >
> > ("!".."~").each do |c|
> > puts c
> > end
> >
> > !
> > "
> > #
> > $
> > %
> > &
> > '
> > (
> > )
> > *
> > +
> > ,
> > -
> > .
> > /
> > 0
> > 1
> > 2
> > 3
> > 4
> > 5
> > 6
> > 7
> > 8
> > 9
> > => "!".."~"
> >
> > This is wrong!
> >
> > ("!".."~").last
> > => "~"
> >
> > This is also right.
> >
> > It looks to me like a problem with the Enumerable mixin but I guess the
> > question is why?
>
> Note this
>
> irb(main):003:0> "9".succ
> => "10"
>
> Try this instead:
>
> (?!..?~).each {|c| p c.chr}
>
> irb(main):008:0> (?!..?~).include? ?> => false
>
>
> Kind regards
>
> robert
>
>
>
> --
> use.inject do |as, often| as.you_can - without end
>
>
Thanks Robert,

That is interesting. I wonder why it doesn't work as you would expect. The
range object seems to be correct (at least through limited include? testing)
however it returns incomplete info whenever you use an Enumerable method or
a method that appears to use an Enumerable method.

--
"Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can't hear a word you're saying."

-Greg Graffin (Bad Religion)

David A. Black

7/2/2008 4:34:00 PM

0

Hi --

On Thu, 3 Jul 2008, Glen Holcomb wrote:

> On Wed, Jul 2, 2008 at 9:12 AM, Robert Klemme <shortcutter@googlemail.com>
> wrote:
>
>> 2008/7/2 Glen Holcomb <damnbigman@gmail.com>:
>>> Okay so when I play with "!".."~" wrong things happen.
>>>
>>> ("!".."~").include?("W")
>>> true
>>>
>>> ("!.."~").include?(" ")
>>> false
>>>
>>> so far so good, however.
>>>
>>> ("!".."~").each do |c|
>>> puts c
>>> end
>>>
>>> !
>>> "
>>> #
>>> $
>>> %
>>> &
>>> '
>>> (
>>> )
>>> *
>>> +
>>> ,
>>> -
>>> .
>>> /
>>> 0
>>> 1
>>> 2
>>> 3
>>> 4
>>> 5
>>> 6
>>> 7
>>> 8
>>> 9
>>> => "!".."~"
>>>
>>> This is wrong!
>>>
>>> ("!".."~").last
>>> => "~"
>>>
>>> This is also right.
>>>
>>> It looks to me like a problem with the Enumerable mixin but I guess the
>>> question is why?
>>
>> Note this
>>
>> irb(main):003:0> "9".succ
>> => "10"
>>
>> Try this instead:
>>
>> (?!..?~).each {|c| p c.chr}
>>
>> irb(main):008:0> (?!..?~).include? ?>> => false
>>
> Thanks Robert,
>
> That is interesting. I wonder why it doesn't work as you would expect. The
> range object seems to be correct (at least through limited include? testing)
> however it returns incomplete info whenever you use an Enumerable method or
> a method that appears to use an Enumerable method.

In 1.9 you get this:

>> ('!'..'~').to_a
=> ["!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-",
".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";",
"<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e",
"f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~"]


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!

Glen Holcomb

7/2/2008 4:57:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Wed, Jul 2, 2008 at 10:34 AM, David A. Black <dblack@rubypal.com> wrote:

> Hi --
>
>
> On Thu, 3 Jul 2008, Glen Holcomb wrote:
>
> On Wed, Jul 2, 2008 at 9:12 AM, Robert Klemme <shortcutter@googlemail.com
>> >
>> wrote:
>>
>> 2008/7/2 Glen Holcomb <damnbigman@gmail.com>:
>>>
>>>> Okay so when I play with "!".."~" wrong things happen.
>>>>
>>>> ("!".."~").include?("W")
>>>> true
>>>>
>>>> ("!.."~").include?(" ")
>>>> false
>>>>
>>>> so far so good, however.
>>>>
>>>> ("!".."~").each do |c|
>>>> puts c
>>>> end
>>>>
>>>> !
>>>> "
>>>> #
>>>> $
>>>> %
>>>> &
>>>> '
>>>> (
>>>> )
>>>> *
>>>> +
>>>> ,
>>>> -
>>>> .
>>>> /
>>>> 0
>>>> 1
>>>> 2
>>>> 3
>>>> 4
>>>> 5
>>>> 6
>>>> 7
>>>> 8
>>>> 9
>>>> => "!".."~"
>>>>
>>>> This is wrong!
>>>>
>>>> ("!".."~").last
>>>> => "~"
>>>>
>>>> This is also right.
>>>>
>>>> It looks to me like a problem with the Enumerable mixin but I guess the
>>>> question is why?
>>>>
>>>
>>> Note this
>>>
>>> irb(main):003:0> "9".succ
>>> => "10"
>>>
>>> Try this instead:
>>>
>>> (?!..?~).each {|c| p c.chr}
>>>
>>> irb(main):008:0> (?!..?~).include? ?>>> => false
>>>
>>> Thanks Robert,
>>
>> That is interesting. I wonder why it doesn't work as you would expect.
>> The
>> range object seems to be correct (at least through limited include?
>> testing)
>> however it returns incomplete info whenever you use an Enumerable method
>> or
>> a method that appears to use an Enumerable method.
>>
>
> In 1.9 you get this:
>
> ('!'..'~').to_a
>>>
>> => ["!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-",
> ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";",
> "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I",
> "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
> "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e",
> "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
> "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~"]
>
>
> David
>
> --
> Rails training from David A. Black and Ruby Power and Light:
> Intro to Ruby on Rails July 21-24 Edison, NJ
> Advancing With Rails August 18-21 Edison, NJ
> See http://www.r... for details and updates!
>
>
Thanks David,

That is good to know. Maybe I should upgrade.

--
"Hey brother Christian with your high and mighty errand, Your actions speak
so loud, I can't hear a word you're saying."

-Greg Graffin (Bad Religion)

Sandor Szücs

7/2/2008 5:10:00 PM

0

Hi,

As David wrote before, have a look at the Ascii-Table.

On 02.07.2008, at 16:48, Glen Holcomb wrote:

> Okay so when I play with "!".."~" wrong things happen.

irb(main):002:0> "!"[0]
=3D> 33
irb(main):003:0> "~"[0]
=3D> 126

>
> ("!".."~").include?("W")
> true

irb(main):001:0> "W"[0]
=3D> 87

>
> ("!.."~").include?(" ")
> false


$ irb
irb(main):001:0> " "[0]
=3D> 32

32 is not between 33 and 126, so include?() does it right.

hth. regards, Sandor Sz=FCcs
--=

Adam Shelly

7/2/2008 5:14:00 PM

0

On 7/2/08, Glen Holcomb <damnbigman@gmail.com> wrote:
> That is interesting. I wonder why it doesn't work as you would expect. The
> range object seems to be correct (at least through limited include? testing)
> however it returns incomplete info whenever you use an Enumerable method or
> a method that appears to use an Enumerable method.
>
It's not Enumerable, it's the Range class's #each method.
In general, Ranges use the <=> operator to test against the first and
last element to determine inclusion, but they use #succ to generate
the next element for #each. Some odd behavior arises because
(s<=>s.succ) != -1 for all s.
I thought that was the whole issue, but it turns out '10' <=> '~' is -1.
It turns out the Range class has special code to deal with Strings -
it also checks that s.succ.length < end.length. Without that, your
example range would enumerate forever.

There has been discussion off and on about changing Range to use some
new alternate method to #succ which would guarantee 'sane' behavoir,
but nothing has ever come of it.

-Adam

Adam Shelly

7/2/2008 5:18:00 PM

0

On 7/2/08, David A. Black <dblack@rubypal.com> wrote:
> In 1.9 you get this:
>
> > > ('!'..'~').to_a
> > >
> => ["!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-",
> ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";",
> "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I",
> "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
> "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e",
> "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
> "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~"]


On 7/2/08, Adam Shelly <adam.shelly@gmail.com> wrote:
> There has been discussion off and on about changing Range to use some
> new alternate method to #succ which would guarantee 'sane' behavoir,
> but nothing has ever come of it.
>
Looks like I spoke too soon. Did Range#each change, or did String#succ?

Robert Dober

7/2/2008 5:41:00 PM

0

On Wed, Jul 2, 2008 at 7:13 PM, Adam Shelly <adam.shelly@gmail.com> wrote:
<snip>
> Some odd behavior arises because
> (s<=>s.succ) != -1 for all s.

Is that so??????

I guess you mean there exists an s for which ...

Cheers
Robert
--
http://ruby-smalltalk.blo...

---
AALST (n.) One who changes his name to be further to the front
D.Adams; The Meaning of LIFF

Adam Shelly

7/2/2008 5:48:00 PM

0

On 7/2/08, Robert Dober <robert.dober@gmail.com> wrote:
> On Wed, Jul 2, 2008 at 7:13 PM, Adam Shelly <adam.shelly@gmail.com> wrote:
> <snip>
> > Some odd behavior arises because
> > (s<=>s.succ) != -1 for all s.
>
> Is that so??????
>
> I guess you mean there exists an s for which ...
>
I'm not sure what you mean

irb(main):027:0> 1<=>1.succ
=> -1
irb(main):028:0> 'a'<=>'a'.succ
=> -1
irb(main):029:0> 'z'<=>'z'.succ
=> 1

Robert Klemme

7/2/2008 6:26:00 PM

0

On 02.07.2008 19:48, Adam Shelly wrote:
> On 7/2/08, Robert Dober <robert.dober@gmail.com> wrote:
>> On Wed, Jul 2, 2008 at 7:13 PM, Adam Shelly <adam.shelly@gmail.com> wrote:
>> <snip>
>>> Some odd behavior arises because
>>> (s<=>s.succ) != -1 for all s.
>> Is that so??????
>>
>> I guess you mean there exists an s for which ...
>>
> I'm not sure what you mean

He meant

You said: for all s: (s<=>s.succ) != -1

He said: there exists at least one s: (s<=>s.succ) != -1

It's probably Robert's mathematical training which made him detect that
the two statements are not equivalent - and he is absolutely right. :-)

Kind regards

robert