Sth. wrong with File.file? (or with me)

That's interesting. Are you suggesting that `if` be a method?

Sure... if we wanted to use real Ruby blocks for the conditionally
executed code. Then we could extend this and have all object
implement false? and true? (Inherited from Object by default)... this
would allow custom false objects... this makes Ruby more friendly to
DSLs which is something I've seen Ruby used for a lot and is a general
__good__ feature of dynamic languages. But I haven't thought about it
enough to make it an RCR.

Brian Mitchell.

Austin Ziegler said:

See, that's why I'm not getting this. [...]

Me too.

Actually, I see #true? as potentially confusing. Imagine explaining to a
newbie why

   if cond

and

   if cond.true?

are not equivalent.

···

--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)

> Ara.T.Howard@noaa.gov said:
> > now i use
> >
> > if TrueClass === options['verbose']
> > @verbosity = 4
> > else
> > @verbosity = Integer options['verbose']
> > end
>
> Hmmm ... why don't you use:
>
> if options['verbose'] == true
> ...

See, that's why I'm not getting this. I don't see the value of:

  if options['verbose'].true?

This is more OOP

over

  if options['verbose'] == true

This is more functional

As a OOP programmer, I will choose the first one.
Call me dumb, I don't care !
:stuck_out_tongue:

···

On Mon, 2004-11-01 at 15:54, Austin Ziegler wrote:

On Tue, 2 Nov 2004 05:47:18 +0900, Jim Weirich <jim@weirichhouse.org> wrote:

The reality is that the implementations of #true? and #false? provided
so far essentially hide that test, making this pure magic. I certainly
don't test against true and false values often enough to warrant these
methods, whereas I do consistently test against nil values.

-austin

o.k. - bad example on my part. i'm more in favour of a bool? and false? test
(especially the false? test to tell difference from nil?). here's an example
of some real code i have:

       def solar_elev_file= value
         @solar_elev_file =
           case value
             when String, TrueClass, FalseClass
               value
             when Fixnum
               unless [0,1].include? value
                 raise ArgumentError, "solar_elev_file <#{ value.inspect }>"
               end
               value
             else
               raise TypeError, "solar_elev_file <#{ value.class }>"
           end
       end

and then later...

       def gen_command
         command = "%s,'%s','%s'" % [program, olsfile, flagfile]

         if solar_elev_file
           case solar_elev_file
             when String
               command << ",solar_elev_file='#{ solar_elev_file }'"
             when TrueClass
               command << ",solar_elev_file=1"
             when FalseClass
               command << ",solar_elev_file=0"
             when Fixnum
               command << ",solar_elev_file=#{ solar_elev_file }"
           end
         end

        ...

in case you haven't guess this code generates a command to send to another
language... it sure is ugly!

i'd rather like to see something like

           case value
             when String, BoolClass
               value

and be able to say things like

   obj = obj.to_s if obj.bool? # map to 'true' or 'false' for idl

or even

   if obj.nil?
     raise 'some error'
   elsif obj.false?
     dont_do_something
   else
     something_else
   end

or even

   bool = str.to_bool

   str = bool.to_str

or even

   truth = obj.to_bool.to_str # map to 'true' or 'false' strings

i understand all the objections like 'why not obj == true' but this makes no
sense if one ever uses obj.nil? does it? after all, you could always do

   if obj == nil

right? but we __like__ obj.nil? don't we? i think it's because you cannot do
this

   if obj = nil # OOPS!

same applies for true?

   if obj = true # OOPS!

of course i've done this many times

   truth = (obj = true) # OOPS AGAIN!

these are hard bugs to find. if i had #true? i'd use it to prevent them:

   truth = obj.true? or other_obj.false? # no bug here

so, in summary, i don't know what the hell i'm saying - just that, to me, it
seems that either:

   - we have nil?, zero?, true?, false?, bool? and a more unified handling of
     truth (TruthClass < BoolClass and FalseClass < BoolClass)

   - we don't have any of it

because testing for nil and zero have got to be just as common/not-common as
testing for false instead of nil or, worse, just as common as accidentally
doing an assignment (=) vs. a comparison (==). to me that reason alone is
reason enough for true? and is better because, unlike compiler warnings,
cannot be ignored or turned off.

anyhow - i hope it's clear i'm just saying i'd like to see a unification of
truth handling in ruby and that true?, false?, bool? and friends would be a
natural __part__ of that unification but by no means the whole thing.

kind regards and sorry for bad examples!

-a

···

On Tue, 2 Nov 2004, Austin Ziegler wrote:

On Tue, 2 Nov 2004 05:47:18 +0900, Jim Weirich <jim@weirichhouse.org> wrote:

Ara.T.Howard@noaa.gov said:

now i use

   if TrueClass === options['verbose']
     @verbosity = 4
   else
     @verbosity = Integer options['verbose']
   end

Hmmm ... why don't you use:

  if options['verbose'] == true
    ...

See, that's why I'm not getting this. I don't see the value of:

if options['verbose'].true?

over

if options['verbose'] == true

The reality is that the implementations of #true? and #false? provided
so far essentially hide that test, making this pure magic. I certainly
don't test against true and false values often enough to warrant these
methods, whereas I do consistently test against nil values.

-

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
When you do something, you should burn yourself completely, like a good
bonfire, leaving no trace of yourself. --Shunryu Suzuki

===============================================================================

Hal Fulton wrote:

  class Array
    def null?
      ! empty?
    end
  end

  class String
    def null?
      ! empty?
    end
  end

  class Hash
    def null?
      ! empty?
    end
  end

  class Fixnum
    def null?
      self == 0
    end
  end

  class File
    def null? # now a little fun
      size == 0
    end
  end

  # And now even more fun

  class Nil
    def null?
      true
    end
    def nnil? # for symmetry
      false
    end
  end

  # And the most fun of all, our beloved nnull?

  class Object
    def null
      false # until overridden
    end
    def nnull?
      ! null? # inherited by everyone
    end
  end

Why not just define #empty? for Fixum, File, NilClass, and Object, and then define nonempty? instead of (argh, can't... make... self... type... this...) nnull? ?

The possibility of confusion between nil and null is too great (for me at least).

Smiley shmiley:

  require 'extensions/object'

  if a.notnil?
    ...
  elsif b.nonnil?
    ...
  end

It strictly concerns 'nil', though -- no concept of emptiness is
entertained.

Gavin

···

On Friday, November 12, 2004, 10:13:04 AM, Hal wrote:

Gavin Sinclair wrote:

unless a.nil?
....
end

  if a.notnil?
    ...
  end

:slight_smile:

You put a smiley, but in all seriousness I've thought of this.
(Actually I'd use nonnil? or nnil?)

"In ruby there have many methods that names end with '!' like, collect!,
compact!. I was so used to that '!' means its return the object after
'affect' and without '!' return the part of the object that was
affected."

The exclamation actually means the operation is destructive and
forever changes the contents of the calling object. It doesn't have
anything to do with the return codes. It's a very nice convention.

Here is a good example:
http://www.rubycentral.com/book/ref_c_string.html#String.chomp

In this case, both examples would return the same string, but one
would change the string it was called against.

···

On Fri, 12 Nov 2004 23:41:00 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:

On Thu, 2004-11-11 at 21:54, Austin Ziegler wrote:

> On Fri, 12 Nov 2004 06:28:51 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:
> > On Thu, 2004-11-11 at 16:15, Austin Ziegler wrote:
> > > On Fri, 12 Nov 2004 05:41:50 +0900, Mohammad Khan <mkhan@lextranet.com> > > > On Thu, 2004-11-11 at 14:50, Austin Ziegler wrote:
> > > No, I didn't. Not anything convincing, at any rate. It seemed to
> > > boil down to "I don't think that a == b is sufficiently OO", which
> > > is certainly not a good reason, IMO. Why do you not like "a ==
> > > true"?
> > Personal taste! May be I am fond of wired taste !!
> > Sorry for trying to share my wired taste with you.
>
> Perhaps it is a matter of taste -- but is this sufficient reason to
> want a change to the language?
Answer is Yes.
If you find something good to you, you will want to share it others.
After share it with others, you might learn that your idea it not as
good as it were to you.

Well, I have learned many of us did not like this #true? and #false?
idea. But I still like it.

> Again, I differentiate between your
> proposal for #true? and #false? and the #iftrue mechanisms, as your
> mechanism:

>
> if a.true?
>
> doesn't really buy anything over
>
> if a == true
>
> for tests of true-value. Wouldn't it be more OO to actually do:
>
> a.iftrue { things-to-do-if-true }.iffalse { things-to-do-if-false }
>
> rather than simply what you've suggested?
>
> > > Yes, we do. I don't think, however, that the Ruby world needs #true?
> > > and #false?
> > well, we need to know first before using something.
> > If you are going to use #true?, you need to know what it does.
> > I am saying this, for your reply about work with others.
> > We really don't have Object#true? in ruby. If you see #true? in my code,
> > you got to look around my code to see what it does?
> > Same way, If I work with you, you might have some new
> > opinion/idea/concept that I am not familiar with. In that case, I will
> > have to do the same.
>
> Mmmm. I donno. I don't think that you'd find anyone else using the
> #true?/#false? mechanism you said. Most other folks would use
> unfamiliar ideas, not unfamiliar and unintuitive syntax. To me, it is
> *not* immediately obvious that #true? and #false? would test against
> == true and == false.
>
> -austin

Sometimes new things are unfamiliar, even existing thing can be
unfamiliar. Unfamiliar doesn't mean wrong. There is no way around other
than learn to be familiar with them.
Here is an example,
In ruby there have many methods that names end with '!' like, collect!,
compact!. I was so used to that '!' means its return the object after
'affect' and without '!' return the part of the object that was
affected. I hope, you guys understood me. English is not my primary
language.
I wrote a script by using Array#delete. I and one of co-worker were
working together. When he saw my code, he said it should be #delete!.
BTW, we are coding in Ruby for about two years. when I wrote that line,
I also wrote '#delete!' ! But after ran my script first time I got to
look at my 'Programming Ruby' book.

Mohammad

--

[mkhan@localhost local]$ rm -Rf /bin/laden

[...]

You put a smiley, but in all seriousness I've thought of this.
(Actually I'd use nonnil? or nnil?)

In fact, when I'm writing code that no one else will see,
<admission type="shameful"/> I sometime use methods like these:

  a1 =
  a2 = [1,2,3]
  s1 = ""
  s2 = "a string"
  h1 = {}
  h2 = {1=>2, 2=>4}

  a1.null? # true
  a1.nnull? # false
  a2.null? # false
  a2.nnull? # true

  s1.null? # true
  h1.null? # true

  # And so on...

See below, and let the flamefest begin.

[...]

    nil.nil? # true
    nil.nnil? # false
    nil.nnnil? # true
    nil.nnnnil? # false

class Object
    def method_missing(name, *args)
      if name.to_s =~ /^n*nil\?$/
        name.to_s.size % 2 == 0 ? nil? : !nil?
      else
        super
      end
    end
end

···

In article <4193F1FB.1060509@hypermetrics.com>, Hal Fulton wrote:

I think this has merit. But I too haven't thought enough about it. One of the
things I would like about it, is that it would help me improve my method
probe, so that I could get some control over conditionals.

If you have time I would love to discuss further on suby-ruby mailing list.

T.

···

On Saturday 30 October 2004 03:19 am, Brian Mitchell wrote:

> That's interesting. Are you suggesting that `if` be a method?

Sure... if we wanted to use real Ruby blocks for the conditionally
executed code. Then we could extend this and have all object
implement false? and true? (Inherited from Object by default)... this
would allow custom false objects... this makes Ruby more friendly to
DSLs which is something I've seen Ruby used for a lot and is a general
__good__ feature of dynamic languages. But I haven't thought about it
enough to make it an RCR.

Jim Weirich wrote:

Austin Ziegler said:

See, that's why I'm not getting this. [...]

Me too.

Actually, I see #true? as potentially confusing. Imagine explaining to a
newbie why

   if cond

and

   if cond.true?

are not equivalent.

Another problem is that a lot (perhaps most) tests within ruby don't return the literal TRUE value.

So if people get used to using

if cond.true?

rather than just

if cond

then they'll be surprised when something like

re = /./
if re.match("a").true? then
puts "match"
end

never prints match

···

--
Mark Sparshatt

[...]
> See, that's why I'm not getting this. I don't see the value of:

    AOL

> if options['verbose'].true?

This is more OOP
>
> over
>
> if options['verbose'] == true

This is more functional

As a OOP programmer, I will choose the first one.
Call me dumb, I don't care !
:stuck_out_tongue:

    Just one question: why on earth would we want "obj.true?" to be different
from "obj" from a boolean perspective? It's _very_ weird.

    You could write aberrations like:

-------------------------------- 8< --------------------------------
if obj.true?
    # Do something
elsif obj
    # Do some other thing
else
    # Do a third thing
end
-------------------------------- >8 --------------------------------

Not very readable to my eyes... and anyway, IMHO, a comparison has nothing to
do with OOP. Objects _can_ be compared, too :slight_smile:

    I don't think "true?" adds anything to the language. If you want to use
"true?" because you find it more readable, why don't you simply extend the
Object class?

    Regards,

···

On Tue, Nov 02, 2004 at 06:05:00AM +0900, Mohammad Khan wrote:

--
Esteban Manchado Velázquez <zoso@foton.es> - http://www.foton.es
EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es

As a newbie, this is also confusing to me

a = Foo.new

if a
# block
end

if a == true
# block
end

and 2nd block doesn't execute !

···

On Mon, 2004-11-01 at 16:02, Jim Weirich wrote:

Austin Ziegler said:
> See, that's why I'm not getting this. [...]

Me too.

Actually, I see #true? as potentially confusing. Imagine explaining to a
newbie why

   if cond

and

   if cond.true?

are not equivalent.

--
Mohammad

> > Ara.T.Howard@noaa.gov said:
> > > now i use
> > > if TrueClass === options['verbose']
> > > @verbosity = 4
> > > else
> > > @verbosity = Integer options['verbose']
> > > end
> > Hmmm ... why don't you use:
> > if options['verbose'] == true
> > ...
> See, that's why I'm not getting this. I don't see the value of:
> if options['verbose'].true?
This is more OOP

I disagree. I don't think it's any more or less OOP than what is below.

> over
> if options['verbose'] == true
This is more functional

I'm not sure that this is the term you wanted -- I don't think that
it's related to functional programming and more related to structured
programming. Remember that OOP is simply building blocks on top of
structured programming to begin with. To me, doing something like:

   if options.verbose.true?

logically leads to the madness of:

   if options.volume.11? # ah, but ours goes to 11!

I can see a *little* more point to the discussion of #ifTrue and
#ifFalse (a la SmallTalk), but I'm still not seeing the ultimate point
of either one.

-austin

···

On Tue, 2 Nov 2004 06:05:00 +0900, Mohammad Khan <mkhan@lextranet.com> wrote:

On Mon, 2004-11-01 at 15:54, Austin Ziegler wrote:
> On Tue, 2 Nov 2004 05:47:18 +0900, Jim Weirich <jim@weirichhouse.org> wrote:

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca
: as of this email, I have [ 5 ] Gmail invitations

The reality is that the implementations of #true? and #false?
provided so far essentially hide that test, making this pure
magic. I certainly don't test against true and false values often
enough to warrant these methods, whereas I do consistently test
against nil values.

o.k. - bad example on my part. i'm more in favour of a bool? and
false? test (especially the false? test to tell difference from
nil?). here's an example of some real code i have:

Note: your code won't do what you expect, will it?

def solar_elev_file= value
  @solar_elev_file =
    case value
    when String, TrueClass, FalseClass
      value
    when Fixnum
      unless [0,1].include? value
        raise ArgumentError, "solar_elev_file <#{ value.inspect }> "
      end
      value
    else
      raise TypeError, "solar_elev_file <#{ value.class }> "
    end
end

Shouldn't that be:

  def solar_elev_file=(value)
    @solar_elev_file =
      case value
      when String
  value
      when true
  1
      when false
  0
      when Fixnum
        raise ArgumentError unless [0, 1].include?(value)
  value
      else
        raise TypeError, "solar_elev_file <#{ value.class }> "
      end
  end

This way, your gen_command never needs to see true or false values.

if obj.nil?
  raise 'some error'

  elsif obj == false

  dont_do_something
else
  something_else
end

The issue I have with a BoolClass is that it's not clearly an
ancestor of TrueClass and FalseClass (although you'll find arguments
from 2002 that suggest that I think it should be; I was wrong), and
there are many possible values for String#to_bool to choose from:

  hai
  iie
  1
  0
  true
  false
  yes
  no
  da
  nyet
  ...

:slight_smile:

i understand all the objections like 'why not obj == true' but
this makes no sense if one ever uses obj.nil? does it? after all,
you could always do

if obj == nil

right? but we __like__ obj.nil? don't we? i think it's because you
cannot do this

Mmmm. I think that it's more common to encounter a nil variable
(e.g., unset) than one that is randomly false or true.

  if obj = nil # OOPS!
same applies for true?
  if obj = true # OOPS!

  if nil == obj
  if true == obj

so, in summary, i don't know what the hell i'm saying - just that,
to me, it seems that either:

- we have nil?, zero?, true?, false?, bool? and a more unified
handling of truth (TruthClass < BoolClass and FalseClass <
BoolClass)

- we don't have any of it

because testing for nil and zero have got to be just as
common/not-common as testing for false instead of nil or, worse,
just as common as accidentally doing an assignment (=) vs. a
comparison (==). to me that reason alone is reason enough for
true? and is better because, unlike compiler warnings, cannot be
ignored or turned off.

I disagree on the commonality. I think that #nil? is a common enough
operation, but I personally rarely use explicit Boolean values.

-austin

···

On Tue, 2 Nov 2004 06:33:50 +0900, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Tue, 2 Nov 2004, Austin Ziegler wrote:

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

Joel VanderWerf wrote:

Why not just define #empty? for Fixum, File, NilClass, and Object, and then define nonempty? instead of (argh, can't... make... self... type... this...) nnull? ?

Yes, nnull? is a pretty ugly name, isn't it? But I dislike long names
such as non_null, not_null, non_empty, aint_null, etc.

Actually, I like "null" better than "empty" -- just a meaningless opinion.

And by the way, my original purpose in defining null? was to use it for
the "nil_or_empty?" that people sometimes use.

The possibility of confusion between nil and null is too great (for me at least).

And they're quite distinct in my mind -- another meaningless opinion
of mine.

Hal

What's wrong with:

   if not a.nil?
     ...
   end

T.

···

On Thursday 11 November 2004 07:30 pm, Gavin Sinclair wrote:

Smiley shmiley:

  require 'extensions/object'

  if a.notnil?
    ...
  elsif b.nonnil?
    ...
  end

Thanks for your nice explanation on exclamatory sign.
And Sorry for my poor English.

See the example below, I was confused with Array#delete! and
Array#delete. Bottom line is, if there have something unfamiliar, I will
have to learn it to make it familiar. There is no other way around.

[mkhan@localhost bin]$ irb
irb(main):001:0> arr = [1, 2, 2, 2, 3, 4]
=> [1, 2, 2, 2, 3, 4]
irb(main):002:0> str = "string"
=> "string"
irb(main):004:0> p arr
[1, 2, 2, 2, 3, 4]
=> nil
irb(main):005:0> p str
"string"
=> nil
irb(main):006:0> arr.delete(2)
=> 2
irb(main):008:0> str.delete("r")
=> "sting"
irb(main):009:0> p arr
[1, 3, 4]
=> nil
irb(main):010:0> p str
"string"
=> nil
irb(main):011:0> str.delete!("r")
=> "sting"
irb(main):012:0> arr.delete!(2)
NoMethodError: undefined method `delete!' for [1, 3, 4]:Array
        from (irb):12
irb(main):013:0> p str
"sting"
=> nil
irb(main):014:0> p arr
[1, 3, 4]
=> nil
irb(main):015:0>

Mohammad

···

On Fri, 2004-11-12 at 13:54, Michael DeHaan wrote:

"In ruby there have many methods that names end with '!' like, collect!,
compact!. I was so used to that '!' means its return the object after
'affect' and without '!' return the part of the object that was
affected."

The exclamation actually means the operation is destructive and
forever changes the contents of the calling object. It doesn't have
anything to do with the return codes. It's a very nice convention.

Here is a good example:
http://www.rubycentral.com/book/ref_c_string.html#String.chomp

In this case, both examples would return the same string, but one
would change the string it was called against.

--

[mkhan@localhost local]$ make love
make: *** No rule to make target `love'. Stop.

Hi --

"In ruby there have many methods that names end with '!' like, collect!,
compact!. I was so used to that '!' means its return the object after
'affect' and without '!' return the part of the object that was
affected."

The exclamation actually means the operation is destructive and
forever changes the contents of the calling object. It doesn't have
anything to do with the return codes. It's a very nice convention.

Bang (!) methods don't have to be destructive; the ! basically means
"dangerous operation -- be careful!" So there can be non-destructive
methods with ! (like exit!), and destructive methods without it (like
Array#pop). A lot of destructive methods are also bang-methods, but
there's no direct correlation.

What this means, in Matz's words, is: "Every bang method has non-bang
(i.e. safer) counterpart. You can totally forget about bang methods
at first."

David

···

On Sat, 13 Nov 2004, Michael DeHaan wrote:

--
David A. Black
dblack@wobblini.net

Hi --

···

On Sat, 13 Nov 2004, Tim Sutherland wrote:

    nil.nil? # true
    nil.nnil? # false
    nil.nnnil? # true
    nil.nnnnil? # false

nil.il? # false

:slight_smile:

David

--
David A. Black
dblack@wobblini.net

don't you think would suprise most newbies too?

   harp:~ > cat a.rb
   re = /./
   if re.match("a") == true
     puts "match"
   end

   harp:~ > ruby a.rb
   (nothing)

i don't think there is anyway around that the fact that newbies must learn that
true (in ruby) is a singleton object and that fact can bite you many ways - and
this is not limited to only #true?

regards.

-a

···

On Tue, 2 Nov 2004, mark sparshatt wrote:

then they'll be surprised when something like

re = /./
if re.match("a").true? then
puts "match"
end

never prints match

--

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
When you do something, you should burn yourself completely, like a good
bonfire, leaving no trace of yourself. --Shunryu Suzuki

===============================================================================