Help me with this block please

Hello, I am fairly new to Ruby and I’m trying to learn it on my own. I
have a block of code that i have a question about, see below:

array = [1, 2, 3, 4]

array.collect! do |n|
n ** 4
end

puts array.inspect

My question is about n4, if i run this code it gives me “[1, 16, 81,
256]”, whats happening here? 4 x 4 is 16 why is the last element 256,
i thought n
4 would multiply the elements by 4. Obviously I was wrong.
Help me understand this Please.

‘**’ is not a multiplication but exponentiation.

In irb shell try to invoke a help if unclear:
irb> 1.class
=> Fixnum
irb> help 'Fixnum#
= Fixnum#

(from ruby core)

**(other)


Exponentiate by other
^^^^^^^^^^^^^^^^^^^^^

Hash symbol ‘#’ after a class name denotes an instance method you are
looking for.

Thank you David that was very helpful

Elysee M. wrote in post #1157663:

Hello, I am fairly new to Ruby and I’m trying to learn it on my own. I
have a block of code that i have a question about, see below:

array = [1, 2, 3, 4]

array.collect! do |n|
n ** 4
end

puts array.inspect

My question is about n4, if i run this code it gives me “[1, 16, 81,
256]”, whats happening here? 4 x 4 is 16 why is the last element 256,
i thought n
4 would multiply the elements by 4. Obviously I was wrong.
Help me understand this Please.

hey elysee try this

array = [1, 2, 3, 4].collect{|element|element*4}
puts array.inspect

hey elysee try this

array = [1, 2, 3, 4].collect{|element|element*4}
puts array.inspect