Number in words Class exercises

Why do I have to name the hashes ONE, TENS, TEENS to work?
I name them one_place , tens_place and use that in the code of course, it doesn’t work.
It says undefined local variable or method `teens’ for 10:Fixnum

It’s impossible to tell from your description…can you post the actual code? (Also, does it throw an error in a REPL, or is it just not passing the spec?)

Thanks for responding to my question. So this is part of my code to illustrate what I mean. So right now this code will not work. It will say “ones_places” not defined. However if I change it to Ones_place, or ONES , it will work. In fact, I can change it to any silly name as long as the first letter is capitalized. I don’t get it. What is the rule? Is it the first letter capitalized?

ones_place = { # Will work if you change it to Ones_place
0 => “zero”,
1 => “one”,
2 => “two”,
3 => “three”,
4 => “four”,
5 => “five”,
6 => “six”,
7 => “seven”,
8 => “eight”,
9 => “nine”
}

tens_place = {
20 => “twenty”,
30 => “thirty”,
40 => “fourty”,
50 => “fifty”,
60 => “sixty”,
70 => “seventy”,
80 => “eighty”,
90 => “ninety”
}

teens = { # will work for Teens
10 => “ten”,
11 => “eleven”,
12 => “twelve”,
13 => “thirteen”,
14 => “fourteen”,
15 => “fifteen”,
16 => “sixteen”,
17 => “seventeen”,
18 => “eighteen”,
19 => “nineteen”
}

magnitues = {
100 => “hundred”,
1000 => “thousand”,
1_000_000 => “million”,
1_000_000_000 => “billion”,
1_000_000_000_000 => “trillion”
}

class Fixnum

def in_words
if self < 10
ones_place[self] #Ones_place
elsif self < 20
teens[self] # will work for Teens
end
end

end

Without seeing the rest of your code, I can only guess what’s going on, but capitalizing the first letter, as far as I know, makes a variable a constant (q.v.) Methods can read values from constants defined outside the method–which is maybe why you’re getting the result you are.

Looking back at my own solution to this exercise, it seems I found it necessary to stick an “x=self” in the method for some reason…it was a couple weeks ago and I can’t remember why I did it, but it might have something to do with a similar issue of scope. I’m still a bit shaky on some of this stuff.

``````def in_words
x = self
if x == 0
return "zero"
end
#more of in_words method
end # of in_words``````
1 Like

James is right about the definition of constants. In Ruby, normal variables defined outside the method are not available inside the method; however, constants (defined with capital letters) are available. This has to do with how Ruby handles scope - the constants become a property of the class they’re defined in.

Generally, you’ll want to pass in any values a method needs in order to operate if they’re not already defined on the object the method is being called on. For example, you’ll almost never want to pass an instance variable to an instance method of the same class. If this doesn’t make sense yet, it will make more sense by the time you reach the end of the Alpha Course and have more experience with classes/objects.