This question already has answers here:
Why use Ruby's attr_accessor, attr_reader and attr_writer?
(5 answers)
Closed 5 years ago.
Pure straightfoward question.
I code in Java and started learning Ruby not long ago. Got this question in my mind.
class Foo
attr_accessor :bar, :baz
end
Based on what I know, I think it automatically sets getters and setters. Just that you access it with the same name.
Related
This question already has answers here:
Is there a way other than traits to add methods to a type I don't own?
(2 answers)
How can I wrap another type and add fields and methods to it?
(3 answers)
How to print a Vec?
(7 answers)
Closed 2 years ago.
I have some struct Animal. I want to implement some methods on Vec<Animal>. I believe the correct way to do this is to create a new wrapper object called Animals. What is the correct object to make this wrapper, though? A struct? An enum? Something else?
This question already has answers here:
What is the usefulness of `enable_shared_from_this`?
(6 answers)
Closed 5 years ago.
What is the point for a class T to inherit from std::enable_shared_from_this<T> ? I can't seem to figure out why you wouldn't just create a std::shared_ptr<this> ?
Cppreference has a good example on why.
If you want to return a std::shared_ptr of this while *this is already owned by a std::shared_ptr and you don't return shared_from_this() but return a new std::shared_ptr<T>(this) instead then you will end up with 2 shared pointers that don't know they're both owning the same object and thus the use_count() will be wrong which will cause a double delete, which is undefined behavior.
This question already has an answer here:
What does self mean in Ruby? [duplicate]
(1 answer)
Closed 6 years ago.
Currently I am reading The Ruby Programming Language and it has mentioning of self in many places and I am not able to understand it's exact use. Is its behaviour similar to this pointer in C++.What all difference those two have?(I am not asking about the pointer dereferencing or any C++ specific things, in general) And when do we use self.something in our ruby code?
self is a variable that points to the object whose scope the current code is in. You would use self.something when calling a class method on that object.
For instance, if you had the following method:
class Foo
def self.bar
puts "Class method!"
end
end
You would call the bar method by calling Foo.bar.
This question already has answers here:
How to make instance variables private in Ruby?
(7 answers)
Closed 8 years ago.
Are instance variables private? I hear both claims that they are private and that they do not have access specifier [sic] though they behave as private. Can instance variables be made private?
There is a limited amount of 'privateness' granted to ruby instance variables, you can always access them from the outside through e.g. instance_variable_get (a public method) as outlined in this question. So depending on your desired level of 'privateness' the answer would be "they are private", "they are protected" or "they cannot be really private"
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get list of a class' methods
Suppose I haven't any reference on hand, and I want to see all the methods in the built-in File class, is that simply available?
---------------------------EDIT---------------------------
answered in Get list of a class' instance methods
Sure, try:
File.methods
And if you have the awesome_print gem installed then it formats the list nicely and provides extra information.