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.
Related
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.
This question already has answers here:
Blocks and yields in Ruby
(10 answers)
Closed 8 years ago.
Yield seems to be neither object nor method. What is it? How does it access the block that is passed as an argument to the method?
yield is a keyword, just like while or end or return.
"How" it accesses the block is not really interesting, no more than "how" a return keyword delivers a value to the calling context, or "how" an end keyword closes a block - unless you want to dive into development of Ruby interpreter itself. The important bit for a Ruby programmer is just that that's what it does.
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"
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Sometimes I see code like
class Thing
def self.add_em(a,b)
a+b
end
end
and sometimes I see
class Thing
def self.add_em(a,b)
#a=a
#b=b
#a+#b
end
end
When/Why should I use the # instance variables instead of just using the parameters as passed?
I believe that one reason is if you want to use those variables in any other method then instance variables will be available and local, parameter based variables will not. However I frequently see # variables being used even though the variables are not being used in any other method.
So I see the pattern of
#a=a
#b=b
at the start of method for all parameters passed in being used a lot but I'm not clear exactly why if they are just used on that method. Is it just a convention in case they are used in other methods?
As you correctly realized, it does not make sense to define instance variables unless they are used in another method. If instance variables are used but are not called in any other method, then that code is probably not written by a good programmer.
But note that sometimes, method definitions are not obvious at first look. For example, if there is
class Thing
attr_reader :a
end
then there actually is a method that uses #a.
I'd say that they did it because they had plans to reference the arguments as instance variables. If not they failed the YAGNI (you aint gonna need it principle). If they changed their minds half way through (which has been known to happen...), they they forgot to tidy up.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
When we try to redefine a constant, Ruby shows only a warning, but not any error. So one can always redefine a constant in Ruby?
Also a private method of a class can be invoked using the send method:
Const = 12
puts Const
#only an warning: already initialized constant Const
Const = 14
puts Const #Displays 14
class MyClass
private
def priv
puts 'In private method'
end
end
obj = MyClass.new
#Error: private method `priv' called for #<MyClass:0x7f2cfda21738> (NoMethodError)
#obj.priv
#but this is fine!
obj.send(:priv)
Are there any rationale behind such designs in Ruby? Do not these violate the basic idea of constants and access specifiers respectively?
Is there any real, practical use of these designs? Some examples would be great if there are!
Note: I do see a lot of questions/discussions here regarding Ruby's constants and private methods, but I did not find anything related to the reason behind these.
As for send, the answer is rather simple: once you use reflection, all bets are off. Note that this is no different than in most other languages as well, you can also circumvent access restrictions in Java using reflection, for example.
And for constants, well, you do get a warning. You do get told that you are doing something you shouldn't. But Ruby is a language which trusts you that you know what you are doing. It won't get in your way. If you want to shoot yourself in the foot, you should be allowed to do that. Or, a more cynical way to look at it: there are so many evil things you can do in Ruby, redefining constants really doesn't matter that much.