So I ran into an interesting dilemma regarding object/receiver cases (upper/lower). Convention states lowercase but something seems to go awry. But, it my case, my code only works when the instance begins with a cap? It's driving me nuts!
I'm am using Start Command Prompt with Ruby. IRB 2.3, and also C9 (problem persisted in the IDE as well)
An example:
#rex.rb
class Dog
def wants(something)
puts "Rex wants to #{something}."
end
end
#rex instance is lowercase, per convention and the book's fidelity
rex = Dog.new
rex.wants("play")
This runs in my machine (via ruby rex.rb) and on irb (via require "rex") as well. It runs. But, then in irb:
rex.wants("x") yields a NameError basically saying rex object is undefined.
To remedy this I have to alter the code:
#rex.rb
class Dog
def wants(something)
puts "Rex wants to #{something}."
end
end
#Rex starts with an uppercase R now, not lowercase r - which it should be
Rex = Dog.new
Rex.wants("play")
I run this similarly on my machine (ruby rex.rb) and on irb (require "rex"). Then, in irb,
Rex.wants("x")
it runs (=> nil) perfectly.
My question: why do I am having to capitalize (the first letter of) my object/receiver? Convention is that they begin with lowercase.
Maybe the book I'm using (Head First Into Rudy) tackles this later on? IDK...the book hasn't touched IRB. I just wanted to experiment. Hopefully others with benefit...google didn't really help me today ;(.
The code runs...but the instance should be lowercase, shouldn't it? What am I missing here?
Is it possible that my machine is screwed up. The codepiece works on ideone when others test it. But it fails for me on my machine and c9? I'm at a loss....