Using underscors or camel case for variables and functions (def)

F

Fily Salas

I thought you couldn't use camel words in Ruby instead you should use
underscores but I just tried it and it works.

Here is the code I tried.

class Calculator
def addIt num1,num2
_num = num1 + num2
puts _num.to_s
end
def subIt num1,num2
_num = num1 - num2
puts _num.to_s
end

end

add = Calculator.new
sub = Calculator.new

add.addIt(5,5)
sub.subIt(5,2)

Is this valid in Ruby or it works but is not semantically correct?

Thanks
 
P

Phillip Gawlowski

I thought you couldn't use camel words in Ruby instead you should use
underscores but I just tried it and it works.

It's a style convention, not something Ruby's parser would enforce.


--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.
 
F

Fily Salas

I probably misunderstood this and honestly I don't remember where I read
that.

Thanks a lot for the clarification!
 
7

7stud --

Fily Salas wrote in post #992819:
I thought you couldn't use camel words in Ruby instead you should use
underscores but I just tried it and it works.

In fact, camel case is recommended for names of constants:

class MyClass #camel case for constants
end

def my_method #snake case
end

my_var = 1 #snake case
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top