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

Ruby

Tobias Rheindorf
Tobias Rheindorf
6,484 Points

Is it really true that methods can't start with an uppercase letter?

It's mentioned in the video that method names can't start with an uppercase letter, but this works fine for me in the console:

def TestFunction puts("function tested"); end

TestFunction();

(I didn't define the method one line, it just displays it like that)

2 Answers

andren
andren
28,558 Points

No, it's not strictly true, but there are consequences to using a capital letter.

Names that start with capital letters are treated as constants. The consequence of that is that to call the function you have to use parenthesis, calling it without parenthesis (which is normally allowed in Ruby) won't work.

So something like this would not work:

def TestFunction 
    puts("function tested"); 
end

TestFunction;

While this:

def testFunction
    puts("function tested"); 
end

testFunction;

Would work.

By convention function names should start with a lowercase letter, and it's generally best to stay with common conventions, especially when you are just starting out with the language.

If you want more info on what is and isn't valid for function names, then you can take a look at this stackoverflow post. It provides quite a bit of detail and also talks about the issue I explained above.

Tobias Rheindorf
Tobias Rheindorf
6,484 Points

Thank you for the clarification! I will keep that in mind.