array as index to array 
Author Message
 array as index to array

Hello list! :-)

Maybe this has been discussed before, but anyway..

If a is an array, a[n] returns the n:th element. a[n..m] returns the
n:th to the m:th element. If b is an array b=[x,y,z], it would be no
surprise if a[b] returned the x:th, y:th and z:th element of a instead
of:

a=(42..97).to_a
b=[3,7,9]
a[b]
TypeError: failed to convert Array into Integer
(irb):17:in `[]'
(irb):17:in `irb_binding'

Or, what is the ruby way of doing this?

Is it annoying to allow things like a[[x,y,z]] to select elemts of an
array?



Wed, 15 Oct 2003 02:24:30 GMT  
 array as index to array
I have the following file:

--begin test.rb--
#! /usr/local/bin/ruby -rdebug

class Myfile
  def initialize(file)


  end

  def close


  end

  def finalize
    self.close
  end

  # Define the destructor
  ObjectSpace.define_finalizer(self, proc {self.finalize})
end
--end test.rb---

When run I get:
 ./test.rb
Debug.rb
Emacs support available.

./test.rb:3:class Myfile
(rdb:1) c
./test.rb:19: `method call on terminated object' (NotImplementedError)

This error only shows up when the -rdebug option is set.
Am I doing something wrong or is this just a de{*filter*} warning?

Thanks

=========================================================
Jim Freeze

---------------------------------------------------------
No comment at this time.
http://www.*-*-*.com/
=========================================================



Wed, 15 Oct 2003 02:31:30 GMT  
 array as index to array

Quote:

> If a is an array, a[n] returns the n:th element. a[n..m] returns the
> n:th to the m:th element. If b is an array b=[x,y,z], it would be no
> surprise if a[b] returned the x:th, y:th and z:th element of a instead
> of:

> a=(42..97).to_a
> b=[3,7,9]
> a[b]
> TypeError: failed to convert Array into Integer
> (irb):17:in `[]'
> (irb):17:in `irb_binding'

> Or, what is the ruby way of doing this?

Somewhat indirectly, you can use indexes (or indices)

  a = (42..97).to_a
  b = [3, 7, 9]

  a.indexes(*b)  #=> [45, 49, 51]

However, your initial suggestion might be interesting to consider as
an RCR.

Dave



Wed, 15 Oct 2003 02:55:45 GMT  
 array as index to array
Hi:

I am trying to add a clase to a list depending upon the
type of the class. Since there are several types of
classes, I am using a case statement. But, the way I
am using the case statement causes the << to call
#to_s, which I don't want it to do.
Here is the error followed by the code.
Can someone please explain this behavior to me?
Tnanks

./dev.rb
./dev.rb:21:in `===': failed to convert Class into String (TypeError)
        from ./dev.rb:21:in `add_device'
        from ./dev.rb:32
        from ./dev.rb:32:in `each'
        from ./dev.rb:32

--begin code--
#! /usr/local/bin/ruby

class DeviceA
  def initialize(a,b)


  end
end

class Assembly
  def initialize

  end
  def add_device(d)
    # This line works

    # These 4 lines cause an error
    case d.type
      when /DeviceA/

    end
  end
end

xdev = []
xdev << DeviceA.new("a","b")
xdev << DeviceA.new("A","B")

assembly = Assembly.new
xdev.each { |x| assembly.add_device x }
--end code--

=========================================================
Jim Freeze

---------------------------------------------------------
No comment at this time.
http://www.freeze.org
=========================================================



Wed, 15 Oct 2003 03:08:18 GMT  
 array as index to array
Hello --

Quote:

> Hi:

> I am trying to add a clase to a list depending upon the
> type of the class. Since there are several types of
> classes, I am using a case statement. But, the way I
> am using the case statement causes the << to call
> #to_s, which I don't want it to do.
> Here is the error followed by the code.
> Can someone please explain this behavior to me?
> Tnanks

> ./dev.rb
> ./dev.rb:21:in `===': failed to convert Class into String (TypeError)
>         from ./dev.rb:21:in `add_device'
>         from ./dev.rb:32
>         from ./dev.rb:32:in `each'
>         from ./dev.rb:32

I don't think it's a matter of #to_s getting called -- but
read on.

[...]

Quote:
>     case d.type
>       when /DeviceA/

>     end

This is in effect performing the test:

   /DeviceA/ === d.type

but Regexp#=== (case equality method for Regexp) expects a String on
the other end of the line, not a Class.

You could force a regexp-to-string test:

  case d.type.to_s
  (etc.)

Or you could do:

  case d
  when DeviceA
    (etc.)

because then your test is:

  DeviceA === d

and Module#===, to paraphrase the pickaxe (p. 345), will return true
if d is an instance of DeviceA or one of DeviceA's descendants.  (The
descendants part may not be desireable, but you can always use the
string-based test above instead.)

David

--
David Alan Black


Web:  http://pirate.shu.edu/~blackdav



Wed, 15 Oct 2003 03:48:38 GMT  
 array as index to array

Quote:

> This is in effect performing the test:

>    /DeviceA/ === d.type

> but Regexp#=== (case equality method for Regexp) expects a String on
> the other end of the line, not a Class.

> You could force a regexp-to-string test:

>   case d.type.to_s
>   (etc.)

I just figured this out.
    case d.type.to_s
    when "DeviceA"

I was hoping to be able to

    case d.type
    when DeviceA

since in an if I can do
 if DeviceA == d.type

but no luck, so, I will stick with your first
suggestion.

Thanks

Quote:
> Or you could do:

>   case d
>   when DeviceA
>     (etc.)

> because then your test is:

>   DeviceA === d

> and Module#===, to paraphrase the pickaxe (p. 345), will return true
> if d is an instance of DeviceA or one of DeviceA's descendants.  (The
> descendants part may not be desireable, but you can always use the
> string-based test above instead.)

=========================================================
Jim Freeze

---------------------------------------------------------
No comment at this time.
http://www.freeze.org
=========================================================


Wed, 15 Oct 2003 03:55:36 GMT  
 array as index to array

Quote:

> Hi:

> I am trying to add a clase to a list depending upon the
> type of the class. Since there are several types of
> classes, I am using a case statement. But, the way I
> am using the case statement causes the << to call
> #to_s, which I don't want it to do.
> Here is the error followed by the code.
> Can someone please explain this behavior to me?
> Tnanks

> ./dev.rb
> ./dev.rb:21:in `===': failed to convert Class into String
> (TypeError)
>     case d.type
>       when /DeviceA/

The good news is you can save some typing here:

      case d
      when DeviceA
         adadsasd

      when DeviceB

etc..

The case statement will do the test

     DeviceA === d

and so on. Class 'Module' defines === to check for instances, so all
this just works out.

Dave



Wed, 15 Oct 2003 04:22:33 GMT  
 array as index to array
Hi,

In message "[ruby-talk:14342] Question about define_finalizer"

|./test.rb:19: `method call on terminated object' (NotImplementedError)
|
|This error only shows up when the -rdebug option is set.
|Am I doing something wrong or is this just a de{*filter*} warning?

You can not access "self" within finalizers, because they are called
AFTER releasing objects.

                                                        matz.



Wed, 15 Oct 2003 09:40:57 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. I have array of indices and array of values then i need to retrieve the values for the indices

2. Index Values of an Array inside a Cluster inside an Array

3. how to access arrays with variable for array names and index

4. RubyChangeRequest #U002: new proper nameforHash#indexes, Array#indexes

5. RubyChangeRequest #U002: new proper namefor Hash#indexes, Array#indexes

6. finding 1d index in 2d array of indices

7. Problem with with Array of U8 to Array of Array of Boolean

8. convert 2d array to 1d array without using shift registers and build array

9. Arrays: Build array in multiple for loops or replace array elements

10. Multidimensional array vs. array of array

11. Question about array ops on arrays of types of arrays of ...(ack)

12. adjustable-array-p, adjust-array and array-destruction

 

 
Powered by phpBB® Forum Software