Destructive! operations

That's not inconsistent; you're just not understanding. Matz posted
about this some time back and its a perfectly clear and consistent
way of working. Let's say you have an empty Array, 'a':

    a =
    a[0] # nil
    a[1] # nil

That's as expected. So why does this do something that you don't
expect?

    a[0..3] #
    a[1..3] # nil

Well, it's easier to look at with a non-empty array. Let's try:

    a = %w(a b c)
    a[0] # a
    a[1] # b
    a[2] # c
    a[3] # nil

Okay, that again is as expected. But where do the indexes point?
According to matz, the indexes are conceptually *between* elements.
Something like:

        a b c
      0^ 1^ 2^ 3^..

So when you start your range outside of the allowable indexes (and
the size of the array is an allowable, but empty, index), then
you'll get +nil+ from the # call. So for our above array, a[0..3]
produces the expected result, a[3..9] produces an empty array, and
a[4..9] produces nil.

It's regular. It's explained. It makes sense. It just doesn't work
like a P language. I doubt you'll find many experienced Ruby
programmers -- or novice Ruby programmers who don't expect Ruby to
act like a P language -- who are confused about this issue.

-austin

···

On Tue, 22 Feb 2005 00:31:42 +0900, Navindra Umanee <navindra@cs.mcgill.ca> wrote:

Austin Ziegler <halostatue@gmail.com > wrote:

On Mon, 21 Feb 2005 06:07:58 +0900, Navindra Umanee >> <navindra@cs.mcgill.ca > wrote:

Often think it would be nice if "" and 0 were treated like nil.
Such functions could then return "". Heck, NilClass.to_s and
NilClass.to_i already return "" and 0 respectively.

I'm far happier that they aren't.

Maybe what is more annoying, is this type of inconsistency:

irb(main):003:0> list[4..5]
=> nil
irb(main):004:0> list[0..3]
=>

Ruby is riddled with these and IMHO it tends to make code that much
more ugly.

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Robert Klemme ha scritto:

I didn't miss this "feature" either. I suspect that an unfinished
transition from Perl to Ruby makes people voice such whishes... :slight_smile:

or an unfinished transition to smalltalk/python :slight_smile:
I don't like the idea of letting objects decide if they are true or not, but it may have some use cases (i.e. for proper proxy objects)

Or maybe Lisp where '() and nil are the same...

-N.

···

Robert Klemme <bob.news@gmx.net> wrote:

I didn't miss this "feature" either. I suspect that an unfinished
transition from Perl to Ruby makes people voice such whishes... :slight_smile:

"Bill Kelly" <billk@cts.com> writes:

From: "Christian Neukirchen" <chneukirchen@gmail.com>

Sometimes, I'd like an "eating nil" that returns itself for each
method call, and is false.

Then, stuff like that would be possible

while line = gets.ignore_if_nil.chomp
  ...
end

Maybe just a crazy idea... :slight_smile:

class NilClass
  def method_missing(meth_id, *args); self; end
end

?

:slight_smile:

ObjectiveC's nil works this way.

I know that.

The downside is you don't find out as early when you
received an unexpected nil, in situations where you
would have preferred to be notified.

I know that too, and that's why I want an explict conversion.

···

Bill

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Brian Schröder <ruby.brian@gmail.com> writes:

On Tue, 22 Feb 2005 00:14:41 +0900, Christian Neukirchen

Then, stuff like that would be possible

while line = gets.ignore_if_nil.chomp
  ...
end

Maybe just a crazy idea... :slight_smile:

What would the ignore_if_nil call in your example do? Wouldn't the
above be equivalent to

while line = gets.chomp
  ...
end

gets returns nil on EOF.

···

Regards,

Brian

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

That's not inconsistent; you're just not understanding. Matz posted
about this some time back and its a perfectly clear and consistent
way of working. Let's say you have an empty Array, 'a':

Thanks for the explanation!

It's regular. It's explained. It makes sense. It just doesn't work
like a P language. I doubt you'll find many experienced Ruby
programmers -- or novice Ruby programmers who don't expect Ruby to
act like a P language -- who are confused about this issue.

I don't really understand why you bring up P languages. It's complex
and unexpected behaviour that you have explained the cause of, but I
fail to see why this particular Ruby Way is more useful or desireable.

Seems mostly like an implementation detail to me but it's good to know
there is an explanation. I probably missed the point. :slight_smile:

Cheers,
Navin.

···

Austin Ziegler <halostatue@gmail.com> wrote:

or an unfinished transition from Javascript

There are a lot of languages where "", 0 and others are treated as non-truth values.
I love that Ruby isn't one of them.

...

But I do support the idea of a single, consistent method across many classes which points out vapid values.

Hey, how about that for a name?
"".vapid? #=> true

:slight_smile:

···

On Feb 21, 2005, at 9:09 AM, gabriele renzi wrote:

Robert Klemme ha scritto:

I didn't miss this "feature" either. I suspect that an unfinished
transition from Perl to Ruby makes people voice such whishes... :slight_smile:

or an unfinished transition to smalltalk/python :slight_smile:

Ooo, this just gave me an idea!

Block scope redefinitions of classes!

withImaginaryHungryNilConstruct do
         while(line = gets.chomp )
              # do stuff with line
         end
end # here NilClasses's definition reverts back to normal

You could do this in Ruby now, the only problem being is it would have
dynamic and not lexical "scope" if I may obscenely misuse a term. This
could be quite useful, I imagine, especially if it was generic (eg.
have a way of saying use these modifications for class X for the
duration of this block).

···

On Tue, 22 Feb 2005 00:27:59 +0900, Bill Kelly <billk@cts.com> wrote:

From: "Christian Neukirchen" <chneukirchen@gmail.com>
>
> Sometimes, I'd like an "eating nil" that returns itself for each
> method call, and is false.
>
> Then, stuff like that would be possible
>
> while line = gets.ignore_if_nil.chomp
> ...
> end
>
> Maybe just a crazy idea... :slight_smile:

class NilClass
  def method_missing(meth_id, *args); self; end
end

?

:slight_smile:

ObjectiveC's nil works this way.

The downside is you don't find out as early when you
received an unexpected nil, in situations where you
would have preferred to be notified.

Regards,

Bill

Yes, and you wanted a nil that eats its messages silently. So it would
also eat chomp. I meant, if you have a all eating nil then
nil.ignore_if_nil.chomp === nil.chomp.

Regards,

Brian

···

On Tue, 22 Feb 2005 00:48:42 +0900, Christian Neukirchen <chneukirchen@gmail.com> wrote:

Brian Schröder <ruby.brian@gmail.com> writes:

> On Tue, 22 Feb 2005 00:14:41 +0900, Christian Neukirchen
>> Then, stuff like that would be possible
>>
>> while line = gets.ignore_if_nil.chomp
>> ...
>> end
>>
>> Maybe just a crazy idea... :slight_smile:
>
> What would the ignore_if_nil call in your example do? Wouldn't the
> above be equivalent to
>
> while line = gets.chomp
> ...
> end

gets returns nil on EOF.

Christian Neukirchen schrieb:

Sometimes, I'd like an "eating nil" that returns itself for each
method call, and is false.

Then, stuff like that would be possible

while line = gets.ignore_if_nil.chomp
...
end

Here you go:

   EATING_NIL = Object.new

   def EATING_NIL.nil?
     true
   end

   def EATING_NIL.method_missing(*)
     self
   end

   class Object
     def ignore_if_nil
       self
     end
   end

   def nil.ignore_if_nil
     EATING_NIL
   end

Regards,
Pit

Navindra Umanee wrote:

I don't really understand why you bring up P languages. It's complex
and unexpected behaviour that you have explained the cause of, but I
fail to see why this particular Ruby Way is more useful or desireable.

Seems mostly like an implementation detail to me but it's good to know
there is an explanation. I probably missed the point. :slight_smile:

I have always had the feeling that there were interesting use cases
for this, but I have never known an example. :slight_smile: It's a bit of behavior
that I've just learned to accept via Matz's explanation. In short,
I'm used to it.

This is in contrast to the fact that certain bang methods sometimes
return nil -- that behavior I've also seen justified, and can easily
imagine use cases, but I still have never liked it. It's one of my
most common bugs, personally. And not once have I ever *wanted* the
"return nil" behavior.

Hal

Ooo, this just gave me an idea!

Block scope redefinitions of classes!

withImaginaryHungryNilConstruct do
         while(line = gets.chomp )
              # do stuff with line
         end
end # here NilClasses's definition reverts back to normal

You could do this in Ruby now, the only problem being is it would have
dynamic and not lexical "scope" if I may obscenely misuse a term. This
could be quite useful, I imagine, especially if it was generic (eg.
have a way of saying use these modifications for class X for the
duration of this block).

You might want to read this:

http://nbtsc.org/~aredridel/2005/02/14-aspects.log

···

Hi --

···

On Wed, 23 Feb 2005, Logan Capaldo wrote:

Ooo, this just gave me an idea!

Block scope redefinitions of classes!

Welcome to the club :slight_smile: See my "Ruby Behaviors" package. as well as
Class-in-State and Import Module (all available on RAA).

It sounds like there might be something that provides this kind of
functionality in Ruby 2.0, in the form of selector namespaces.

David

--
David A. Black
dblack@wobblini.net

Brian Schröder <ruby.brian@gmail.com> writes:

Brian Schröder <ruby.brian@gmail.com> writes:

> On Tue, 22 Feb 2005 00:14:41 +0900, Christian Neukirchen
>> Then, stuff like that would be possible
>>
>> while line = gets.ignore_if_nil.chomp
>> ...
>> end
>>
>> Maybe just a crazy idea... :slight_smile:
>
> What would the ignore_if_nil call in your example do? Wouldn't the
> above be equivalent to
>
> while line = gets.chomp
> ...
> end

gets returns nil on EOF.

Yes, and you wanted a nil that eats its messages silently. So it would
also eat chomp. I meant, if you have a all eating nil then
nil.ignore_if_nil.chomp === nil.chomp.

I want a special eating nil, not the standard nil eat everything
(because that can get harmful easily).

···

On Tue, 22 Feb 2005 00:48:42 +0900, Christian Neukirchen > <chneukirchen@gmail.com> wrote:

Regards,

Brian

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Pit Capitain <pit@capitain.de> writes:

Christian Neukirchen schrieb:

Sometimes, I'd like an "eating nil" that returns itself for each
method call, and is false.

Then, stuff like that would be possible

while line = gets.ignore_if_nil.chomp
...
end

Here you go:

   EATING_NIL = Object.new

   def EATING_NIL.nil?
     true
   end

   def EATING_NIL.method_missing(*)
     self
   end

   class Object
     def ignore_if_nil
       self
     end
   end

   def nil.ignore_if_nil
     EATING_NIL
   end

Unfortunately, that would make the loop turn endless, as:

  irb(main):021:0> puts "Doesn't work as I want" if EATING_NIL
  Doesn't work as I want

It can't be done in Ruby-only, I think.

···

Regards,
Pit

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

Ahh, good to know I'm not alone in the world!

Btw, in case anyone gets the wrong idea, all this complaining just
means I just love Ruby more. :slight_smile:

Cheers,
Navin.

···

Hal Fulton <hal9000@hypermetrics.com> wrote:

I have always had the feeling that there were interesting use cases
for this, but I have never known an example. :slight_smile: It's a bit of behavior
that I've just learned to accept via Matz's explanation. In short,
I'm used to it.

This is in contrast to the fact that certain bang methods sometimes
return nil -- that behavior I've also seen justified, and can easily
imagine use cases, but I still have never liked it. It's one of my
most common bugs, personally. And not once have I ever *wanted* the
"return nil" behavior.

ditto - foo.gsub!(...).gsub!(...).gsub!(...).damn.

···

On Feb 21, 2005, at 5:10 PM, Hal Fulton wrote:

This is in contrast to the fact that certain bang methods sometimes
return nil -- that behavior I've also seen justified, and can easily
imagine use cases, but I still have never liked it. It's one of my
most common bugs, personally. And not once have I ever *wanted* the
"return nil" behavior.

Darn, now I don't feel nearly as smart. Oh well, the upside is I don't
have to write the code myself :wink:

···

On Wed, 23 Feb 2005 21:45:53 +0900, David A. Black <dblack@wobblini.net> wrote:

Hi --

On Wed, 23 Feb 2005, Logan Capaldo wrote:

> Ooo, this just gave me an idea!
>
> Block scope redefinitions of classes!

Welcome to the club :slight_smile: See my "Ruby Behaviors" package. as well as
Class-in-State and Import Module (all available on RAA).

It sounds like there might be something that provides this kind of
functionality in Ruby 2.0, in the form of selector namespaces.

David

--
David A. Black
dblack@wobblini.net

You might want to read this:

http://nbtsc.org/~aredridel/2005/02/14-aspects.log

Nice! where can I download >:-)

Quite interesting. I never thought of AOP in that manner before. (Not
that I've done much more with AOP than read some overviews of it.)

···

On Wed, 23 Feb 2005 15:52:04 +0900, Aredridel <aredridel@gmail.com> wrote:

You might want to read this:

http://nbtsc.org/~aredridel/2005/02/14-aspects.log