G
G
Gabe Jonson2015-12-11 11:40:09
Ruby on Rails
Gabe Jonson, 2015-12-11 11:40:09

Class Name & @?

Good afternoon dear! Who can explain to me such a thing, what is this

class Name

end
and what is
def Name
 @var = var
end

I read it in ruby-lang, I didn’t understand, I asked the PHP developer;), well, in order to draw an analogy, he somehow couldn’t really explain what it was ...
def
it’s understandable, the analogy is in js, but the class is not clear ; ( , thought at first that it was something like but, I realized that this is nonsense ... ;(
function Name() {}
@var
return

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene Burmakin, 2015-12-11
@Freika

You need to read about Ruby's classes, instance variables, and methods.
habrahabr.ru/post/48756
And yes - asking a PHP developer about Ruby is not a good idea.

A
Artem Pyankov, 2015-12-11
@ibub1ik

A class is an entity that encapsulates data and its behavior. If we draw an analogy with js, then a class is a prototype (only in addition to behavior, it also stores data).
`@var` is an instance variable and is visible when the execution context is a particular instance of the class. Also in Ruby, the last expression evaluated is the result of the function. Similar to a function local variable in js. Something like this:
function Name() { return var = 1; }

A
asd111, 2015-12-11
@asd111

EcmaScript 6 (modern javascript) also introduced classes.
Try to run

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>
    "use strict";
    class Polygon {
        constructor(height, width) {
            this.height = height;
            this.width = width;
        }

        calcArea() {
            return this.height * this.width;
        }
    }
    var p = new Polygon(10, 20);
    alert("height = " + p.height + ", width = " + p.width + ", area = " + p.calcArea());
</script>
</body>
</html>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question