Reputation: 10541
What's the best way to return the value of the 'hash' variable?
define_method :hash_count do
char_count = 0
while char_count < 25 do
hash = ''
hash << 'X'
char_count += 1
end
end
Upvotes: 0
Views: 314
Reputation: 43298
You have to define hash
outside the loop. If it's inside you keep resetting it on every iteration.
define_method :hash_count do
char_count = 0
hash = ''
while char_count < 25
hash << 'X'
char_count += 1
end
hash # returns the hash from the method
end
By the way, you don't have to keep track of the char_count
. Just check the length of the string:
define_method :hash_count do
hash = ''
hash << 'X' while hash.length < 25
hash # returns the hash from the method
end
Upvotes: 1