Ruby: Method Help -


i computer science major, , learning ruby. lost on problem supposed solve, syntax issues. here do:

write method takes array of strings , block , calls block on each string. recall keyword call block yield. syntax call following:

method(["blah", "blah"]) {...} 

test method passing block prints result of applying reverse each string. print out original array after call. test again passing block calls reverse!. print out original array. observe differences, explain them in comments.

i'm not sure how problem @ all. i'm new block , yield.

def my_method(array, &block)   array.each{|a| yield a} end  array = ["one", "two", "three"] my_method(array) |a|   puts a.reverse end #=> eno #=> owt #=> eerht array #=> ["one", "two", "three"] my_method(array) |a|   puts a.reverse! end #=> eno #=> owt #=> eerht array #=> ["eno", "owt", "eerht"]  

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -