3

Classes, their methods, their variables etc. have been a very new concept for me and also something that has been difficult for me to fully grasp.

I'm teaching myself Ruby, and this topic is frustrating so I decided to look at things differently.

I realize I've been using classes, instance variables and instance methods from day one.

Example

**String - class

"Hi my name is...." - instance of the String class.

.length - an instance method for the String class that I can call on the above instance.

"Hi my name is....".length => 17

I could create

x = String.new("Hi")

But I can also create

x = "Hi"

Both are the same

If I created a cat class

class Cat 
  attr_reader :name

  def initialize(name)
    @name = name 
  end 

end

jinx = Cat.new("Jinx")
print jinx.name => Jinx

Here we have

Cat - class

"Jinx" - instance of the Cat class.

.name - an instance method for the Cat class.

Two questions

Is this a good way to view classes? When I get to bigger problems like creating a tic tac toe game (which I am doing now) with multiple classes I feel over my head at times. It's frustrating, cause I feel stupid (maybe I should get used to that), but I'm beating myself up here. I've had a very easy time going on code wars and solving a ton of problems with a single method. I know somewhere if I can relate that to classes, I'd better understand. Is looking at the solution code when stuck the best way to teach myself? I don't have anyone to ask and I want to retain as much info as possible.

Second, how does Ruby know a String is a String, an Array an Array, a Hash a Hash so I can define those variables and not call .new like I would for Cat? Is it just the syntax "" for string, [] for array, {} for hash so it assumes that's what we are talking about?

hancho
  • 1,345
  • 3
  • 19
  • 39
  • seems to me like you're correct. In terms of how `"string"` is know to be a string - yes, it's because of the quotations; these are called "literals" and there's a list [here](https://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Literals) - though of them you'll not find a need for. – max pleaner Jun 17 '17 at 04:29
  • check this out https://stackoverflow.com/questions/9224517/what-are-classes-references-and-objects – z atef Jun 17 '17 at 05:01
  • you can think of "text_here" as basically a type of constructor for a string object - you can think of it like a short cut. Secondly, jinx LOWERCASE is a reference to an instance of the Cat class, not Jinx capitalised - just gotta be explicitly clear o n this point. – BenKoshy Jun 17 '17 at 06:07

2 Answers2

7

The concept you're looking for is Literals, but to explain it in more detail, let's start with your example code.

String.new

There are actually two different strings in this code:

x = String.new("Hi")

"Hi" is a string object, and then a second string object is created and stored in x. You can see this better with this equivalent code:

y = "Hi"
x = String.new(y)

The x and y variables hold two different strings objects, even though both objects contain the same contents. You can prove that they are different by modifying one of them, and checking that the other one stays the same

y.downcase!
y #=> "hi"
x #=> "Hi"

Cat.new

Considering this code:

jinx = Cat.new("Jinx")

When you say that "Jinx" is a Cat, that's not quite right. "Jinx" is still a string. Cat.new will create a new Cat object, and it will store the "Jinx" string inside the object.

You can see the difference more clearly like this:

# this is a string
name = "Jinx"
name.length #=> 4

 # this is a Cat, with a string inside it
jinx = Cat.new(name)
jinx.name #=> "Jinx"
jinx.name.length => 4

The Difference

The difference between the two is that Ruby has string literals but not Cat literals.

A literal is an object that you can type into code, without actually creating it with new. Here are some examples of literals:

  • "Hello" makes a String object
  • 5 makes an Integer object
  • 5.5 makes a Float object
  • [] makes an Array object
  • {} makes a Hash object

You can see a full list of Ruby literals here: https://ruby-doc.org/core-2.3.0/doc/syntax/literals_rdoc.html

There is no literal for Cat objects, because it's a custom class you created youself. I can't just type in something like <Jinx> to create a Cat object because Ruby will not understand that -- I have to create it with Cat.new.

Tom Dalling
  • 23,305
  • 6
  • 62
  • 80
1
  1. Don't beat yourself up over it. I know it feels overwhelming. It happens. It happens more when you teach yourself and don't have someone else who knows it to help you. Programming is tricky, it's just the nature of the beast.

  2. I don't know Ruby, but the great thing about programming is it's only about 15% knowing the language and a lot of the thought process of coding (and a lot more debugging) so I don't need to to help you here. It knows what you are using based on the syntax of what you declare with, or the symbols like "...", [ ], and { }

cMcNerlin
  • 112
  • 8
  • 1
    That it is more like an advice, rather than an answer to the question :) – z atef Jun 17 '17 at 04:55
  • The first part is indeed advice, but the second part of my answer actually addresses the question asked. Thank you for your attention to detail though :) – cMcNerlin Jun 17 '17 at 04:57