Can somone help me with the .collect method?


#1


https://www.codecademy.com/courses/ruby-beginner-en-L3ZCI/0/2?curriculum_id=5059f8619189a5000201fbcb


fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

Add your code below!

This is what i did.

doubled_fibs = fibs.collect { |p| puts p * 2 }

And it tells me that the number 2 isnt included in the array. Then when i add a 2 to the array it tells me to not modify array lol.


Replace this line with your code.


#2

@codecrimson,

Read the definition

The collect method takes a block 
and applies 
the expression in the block 
to every element in an array.

So you want to double all elements of an Array
you then use

doubled_fibs = fibs.collect { |p| p * 2 }

#3

Hi man i have a new problem since i finished the challenge you answered mind helping me with it?

https://www.codecademy.com/courses/ruby-beginner-en-L3ZCI/1/2?curriculum_id=5059f8619189a5000201fbcb


#4

@codecrimson
Please read
http://stackoverflow.com/questions/6167619/i-dont-get-the-idea-behind-ruby-proc-why-not-just-use-normal-method
and
http://stackoverflow.com/questions/4911353/best-explanation-of-ruby-blocks

Present the FULL code you are using now
and
specify what you do not understand.....


#5

This is the full code......

floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911]
# Write your code below this line!

round_down = proc.new {|o| o.floor }
# Write round_down = proc.New {|o| o.floor }your code above this line!
ints = floats.collect(&round_down)

Code academy respondes by saying tried to create probject without block.


#6

@codecrimson
Try it by using
Proc.new


#7

As you can see i used proc.new its next to the floor block.


#8

@codecrimson, your block is not complite as you should to create integers from the numbers in array. I guess that there are also some different ways to get this purpose. This one works for me:

round_down = Proc.new { |n| n=n-(n % 1)}