Saturday, 15 May 2010

Return length of strings in array with ruby -



Return length of strings in array with ruby -

i'm beginner programming , given problem solve ruby:

given array containing strings, homecoming array containing length of strings. supposed write method named 'length_finder' accomplish task.

i wrote:

def length_finder(input_array) = [ ] input_array.each { |n| puts n.length.to_i } << n end

i got error undefined local variable or method `n'. what's wrong code?

since assignment, won't give direct reply problem. instead, i'll show similar. suppose wanted create array consisting of first character of each word in given array:

def first_char(x) x.map {|s| s[0]} end y = first_char(["abc", "xyz", "def"]) # y => ["a", "x", "d"]

replace s[0] suitable manipulation problem.

try (and own method) in irb, interactive ruby environment. exploring 1 of best ways learn, , irb makes exploring easy.

ruby

No comments:

Post a Comment