 |
|
| Computers Forum Index » Computer Languages (Ruby) » Array#collect in a method call, not working for me... |
|
Page 1 of 1 |
|
| Author |
Message |
| Michael Randall... |
Posted: Mon Oct 26, 2009 5:17 am |
|
|
|
Guest
|
I am sure I'm making a newbie mistake, as I've just started learning
Ruby, and would really appreciate someone pointing out what I've done
wrong, why it is wrong, and how it *should* be done. Thanks in advance.
I am writing a method definition to double any number, or all numbers if
an array of numbers is passed to it. When I use array.collect outside of
the method, it works as expected. When I place it inside of the method,
instead of multiplying each integer, it treats the entire array as one
object and double it, [1, 2, 3, 4] becoming 12341234.
I've attached my test code, "simplea.rb", and below is the output when I
run it. In case it matters to anyone, I'm running Ruby 1.8.7 under
Cygwin (because I hate windows and don't have a Mac yet). ;)
$ simplea.rb
Test double([1, 2, 3, 4])
12341234
Test double( 3 )
6
Let's prove it.
2
4
6
8
Attachments:
http://www.ruby-forum.com/attachment/4184/simplea.rb
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Michael Randall... |
Posted: Mon Oct 26, 2009 11:44 pm |
|
|
|
Guest
|
OK, I *thought* that part was working, but now I see that it wasn't
really. So to help me understand I wrote the following, to see what it
does and I see that adding the #inspect will give me the string to
compare against.
But does that mean that (a = [1, 2].class) is setting a to be an Array?
Oh, and thank you!
Here is my test...
irb(main):034:0* def proovy( n )
irb(main):035:1> a = n.class.inspect
irb(main):036:1> b = n.class
irb(main):037:1> puts "a: #{a}"
irb(main):038:1> puts "b: #{b}"
irb(main):039:1> case a
irb(main):040:2> when "Array" : puts "a matched Array in case"
irb(main):041:2> when "Fixnum" : puts "a matched Fixnum in case"
irb(main):042:2> end
irb(main):043:1> case b
irb(main):044:2> when "Array" : puts "b matched Array in case"
irb(main):045:2> when "Fixnum" : puts "b matched Fixnum in case"
irb(main):046:2> end
irb(main):047:1> end
=> nil
irb(main):048:0> proovy(1)
a: Fixnum
b: Fixnum
a matched Fixnum in case
=> nil
irb(main):049:0> proovy([1, 2, 3])
a: Array
b: Array
a matched Array in case
=> nil
Robert Klemme wrote:
Quote: 2009/10/26 Michael Randall <randallsata at (no spam) gmail.com>:
I've attached my test code, "simplea.rb", and below is the output when I
Test double( 3 )
Attachments:
http://www.ruby-forum.com/attachment/4184/simplea.rb
There are a few issues with the first line:
my_a = [ n ].to_a unless n.class == "Array"
First, the test will always fail because the class and the name of the
class are of different types:
irb(main):002:0> Array == "Array"
=> false
[ ... ]
robert
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Michael Linfield... |
Posted: Tue Oct 27, 2009 12:03 am |
|
|
|
Guest
|
Quote: But does that mean that (a = [1, 2].class) is setting a to be an Array?
Consider this:
Quote: n = []
=> []
a = n.class
=> Array
a << 1
NoMethodError: undefined method `<<' for Array:Class
a doesn't become an array, it becomes the class type of Array.
Regards,
- Mac
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
| Michael Randall... |
Posted: Tue Oct 27, 2009 12:11 am |
|
|
|
Guest
|
Michael Linfield wrote:
Quote: But does that mean that (a = [1, 2].class) is setting a to be an Array?
Consider this:
n = []
=> []
a = n.class
=> Array
a << 1
NoMethodError: undefined method `<<' for Array:Class
a doesn't become an array, it becomes the class type of Array.
Regards,
- Mac
That makes sense. And I don't know why I didn't think to do the
following to test it myself...
irb(main):061:0> n = []
=> []
irb(main):062:0> a = n.class
=> Array
irb(main):063:0> a.class
=> Class
THANKS!!
--
Posted via http://www.ruby-forum.com/. |
|
|
| Back to top |
|
|
|
|
|
All times are GMT
The time now is Fri Dec 04, 2009 2:53 pm
|
|