[#4766] Wiki — "Glen Stampoultzis" <trinexus@...>

21 messages 2000/09/04
[#4768] RE: Wiki — "NAKAMURA, Hiroshi" <nahi@...> 2000/09/04

Hi, Glen,

[#4783] Re: Wiki — Masatoshi SEKI <m_seki@...> 2000/09/04

[#4785] Re: Wiki — "NAKAMURA, Hiroshi" <nakahiro@...> 2000/09/05

Howdy,

[#4883] Re-binding a block — Dave Thomas <Dave@...>

16 messages 2000/09/12

[#4930] Perl 6 rumblings -- RFC 225 (v1) Data: Superpositions — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/09/15

[#4936] Ruby Book Eng. translation editor's questions — Jon Babcock <jon@...>

20 messages 2000/09/16

[#5045] Proposal: Add constants to Math — Robert Feldt <feldt@...>

15 messages 2000/09/21

[#5077] Crazy idea? infix method calls — hal9000@...

This is a generalization of the "in" operator idea which I

17 messages 2000/09/22

[#5157] Compile Problem with 1.6.1 — Scott Billings <aerogems@...>

When I try to compile Ruby 1.6.1, I get the following error:

15 messages 2000/09/27

[ruby-talk:03242] Re: Questions re: "new" "+=" etc.

From: matz@... (Yukihiro Matsumoto)
Date: 2000-09-07 16:23:24 UTC
List: ruby-talk #3242
Hi,

In message "[ruby-talk:03234] Questions re: "new" "+=" etc."
    on 00/06/09, hal9000@hypermetrics.com <hal9000@hypermetrics.com> writes:

|Firstly:
|
|I was trying to create an object that could be added
|to itself. I tried to implement the + operator, and
|I discovered that I could not invoke "new" from
|within the class -- see line (e). I assume this is
|a feature -- but why?

You have to know that instance methods and class methods are totally
different things in Ruby, unlike some languages.  (e) tries to invoke
an instance method named `new', not a class method.

|  def + (other)
|    case other.type.to_s
|      when "Wocka"
|        Wocka(@data + other.data)           # Line (b)
|#        self.type.new(@data + other.data)  # Line (c)
|#        type.new(@data + other.data)       # Line (d)
|#        new(@data + other.data)            # Line (e)
|      when "Fixnum"
|        Wocka(@data + other)
|    else
|      raise "Adding an unsupported type to a Wocka object."
|    end
|  end

|Secondly:
|
|Since I implemented the + operator, the += operator
|should have been created for me. (And it was, as I
|confirmed.)

First of all, += is not a redefinable operator methods.  a += b is a
syntax sugar for a = a + b.  This does not mean Ruby create a +=
method automagically.  Since it's not a method, defined? does not
check += method.

And I feel something weird if a + b alters the status of a.  For
example, Array#+ returns a concatenated array, but does not alter the
operands, on the other hand, Array#concat append the element to the
array.

|Thirdly:
|
|Line (i) requires parens -- not sure why -- see line (j).
|This tells me Wocka::+ is NOT defined.

Lines (i) and (j) checks for a class method named `+', which is not
defined.  Line (k) checks for `+=', which is not a valid method name. 
This explains why lines (k) and (m) raises error.

Line (l) checks for x's instance method named `+', which is what you
want to check, thus returns the value you expected.

|# if defined? Wocka::+                   # Line (i)
|if defined? (Wocka::+)                 # Line (j)
|  print "Wocka::+ is defined\n"
|else
|  print "Wocka::+ is NOT defined\n"
|end
|
|# if defined?(Wocka::+=)                 # Line (k)
|#   print "Wocka::+= is defined\n"
|# else
|#   print "Wocka::+= is NOT defined\n"
|# end
|
|
|if defined?(x.+)                       # Line (l)
|  print "x.+ is defined\n"
|else
|  print "x.+ is NOT defined\n"
|end
|
|# if defined?(x.+=)                    # Line (m)
|#   print "x.+= is defined\n"
|# else
|#   print "x.+= is NOT defined\n"
|# end

Hope this helps

							matz.

In This Thread