Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Lawrence Chang
4,659 PointsIs this variable an array?
'''ruby def old_roman(num) roman = ' ' # code in question
roman << 'M' * num
roman end '''
roman = ' ' is this a method to make a new array? thank you!
4 Answers
Sebastien Ferre
3,319 PointsHi Lawrence,
I'm not sure to understand everything in your code but here are two ways to make a new array :
- Make an instance of the array class
roman = Array.new
- Declare a variable as an empty array
roman = []
Once you have created a new array you can push things inside as follow :
roman << 'thing'
So I think your method old_roman(num) would rather look like :
def roman(num)
roman = []
roman << 'M' * num
end
You can find all the details about array in the array documentation I hope it will be helpfull.
Lawrence Chang
4,659 PointsSebastien,
Thanks for the response! Yeah that is what I thought too. Below is a ruby code from Chris Pine's Learn to Program book. The program is to write a method that when passed an integer it returns a string containing the proper Roman numeral.
def old_roman_numeral num raise 'Must use positive integer' if num <= 0 roman = '' roman << 'M' * (num / 1000) roman << 'D' * (num % 1000 / 500) roman << 'C' * (num % 500 / 100) roman << 'L' * (num % 100 / 50) roman << 'X' * (num % 50 / 10) roman << 'V' * (num % 10 / 5) roman << 'I' * (num % 5 / 1) roman end puts(old_roman_numeral(1999)) =>MDCCCCLXXXXVIIII
line 3 threw me off since I have not seen that syntax before.
Lawrence Chang
4,659 Pointsby the way side note how did you make your code come out like that in the forum hahaa. Thanks for all the help!!
Sebastien Ferre
3,319 PointsI'm glad this helped! ;) You actually have to wrap your code into 3 backtick characters. I'm quite sure you used 3 quotation marks instead of backticks, and that's why it's does not appear the good way.