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

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

On Thu, Apr 14, 2011 at 8:11 PM, Fily S. [email protected]
wrote:

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 G.

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.

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

Thanks a lot for the clarification!

On Fri, Apr 15, 2011 at 03:11:48AM +0900, Fily S. wrote:

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

Should is the operative word – not must.

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

It works fine. It’s just harder to read at a glance than with
underscores for most contexts with method and variable names.

Thank you all very much!

Fily S. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs