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
Aurelian Spodarec
7,369 PointsNot sure what is wrong
Hello, Im trying to work out this code but not sure what is wrong with it .
print " Hello what your name? "
first_name = gets.chomp
first_name.capitalize!
print " How old are you?"
age = gets.chomp
age = capitalize!
def age == 17
print " Im 17 years too!"
elsif age > 17
print " Oh ho ho ho , im older than you :D"
elsif age < 17
print " Your older than me ;/"
else
print " YOu should write in number next time"
end
Aurelian Spodarec
7,369 Pointsi know it shoulld be elsif but it still doesnt work
Aurelian Spodarec
7,369 Pointsthank you , now i know how to post the code , i checked what you did :) thank you
2 Answers
John Steer-Fowler
Courses Plus Student 11,734 PointsYou issue here is that you are using def to define a method called age, rather than using an if statement.
Try changing the def to if
if age == 17
print " Im 17 years too!"
elsif ( age > 17 )
print " Oh ho ho ho , im older than you :D"
elsif ( age < 17)
print " Your older than me ;/"
else
print " YOu should write in number next time"
end
Aurelian Spodarec
7,369 Pointsit works :D but when i type 17 theres some error
John Steer-Fowler
Courses Plus Student 11,734 PointsOkay, also I noticed you are using gets.chomp for getting an integer value (17).
Try using something like gets.chomp.to_i to change the string from gets (get string) to an integer.
Aurelian Spodarec
7,369 Pointsthank you :D and the last problem was that i put age capitalize even in wrong syntax and it didtn work , now it works :D
John Steer-Fowler
Courses Plus Student 11,734 PointsAlso Aurelian Spodarec,
There is no need to use capitalize! on the age variable. This is an integer, so does not need to be capitalized.
Aurelian Spodarec
7,369 PointsCould you tell me when do we use a method like def and end ? what is this for ? i thought inside def the if and else statement goes in
John Steer-Fowler
Courses Plus Student 11,734 PointsOkay. Well you use def to define your own methods. Ruby has some built in methods that are ready to be used, such as .gets is a method. By using def you can create your very own method that can be called whenever you need it.
For example:
def bark
puts "Woof!"
end
Here I am defining a really simple method that does not already exist in Ruby. You can call this method later on in your program to put the word "Woof!" to the screen. For example, you could have an instance called dog and you could call the bark method on the dog like this dog.bark and it will put "Woof!".
Hope this gives you a good idea of how methods are defined and why they are used.
Aurelian Spodarec
7,369 PointsA i think i get it . Its like as you said gets is a build in method that will take our input in same as if we make a method def and we call it like bark and puts woof! we could add a sound in it and whenever we put bark in our code , it will write woof! with the dog sound . Its like creating new things with def if i understood it well.
John Steer-Fowler
Courses Plus Student 11,734 PointsYes, you have the right idea :D
Creating our own methods is really important in programming. It's a great way to code faster. Also, Less is More in Ruby programming. People often think that writing hundreds of thousands of lines of code is really cool and epic, but in fact the best programmers write the same program but with much less code. Using really well made methods is one way of reducing the amount of code but achieving the same outcome in programming.
Aurelian Spodarec
7,369 Pointsintresting , i see, the code will be alot cleaner and it could save days or even weeks or more as you said less is more in ruby programming . Thank you :) ill try now to aply that when making HTML and CSS webpages. Not that something is wrong with them but sometimes i just rewrite the code : p but i thought it doesnt matter much
John Steer-Fowler
Courses Plus Student 11,734 PointsJohn Steer-Fowler
Courses Plus Student 11,734 PointsI formatted your code for you for use in your question. If you are struggling to format code properly, take a look at the Markdown Cheatsheet when posting a question.