I want to define an instance method of a class in a module that can only be accessed from within the same module in RubyApplicable source code
module Sample class A def foo puts 'boo' end def bar self.foo end end class B def bar A.new.foo end end end
For the above code,
A.new.bar #->'boo' B.new.bar #->'boo' A.new.foo #->I get an error or exception
Supplement (what you really want to do)
I want to be
Is it close to the state without the access modifier in Java?
Class A: represents a certain data structure
Class B: Create an instance of A based on external input
I want to call various methods (mainly various setters) of A from B to A.
However, we want to prevent these setters from being called from the outside (to eliminate unexpected usage), and what we really want to do.
So, in that case, you can answer that it is a classic way to write in this way in Ruby.
Answer # 1
I want to prevent these setters from being called from outside (I want to eliminate unexpected usage)
It isimpossibleto do strictly in Ruby. Because if you use
__ send __, you can call
private, and even a method called
instance_variable_setis available. is.
If you want to ignore people who intentionally call
private(just don't inadvertently use that method), you can call the method
privateand call it
__ send __.
- i want to use exit outside the ruby class to exit the method processing
- ruby - how to define getter method dynamically with rails
- ios - i want to call the urlsession class from the searchbuttonclicked method
- ruby - in rails52, i want to get the value of the related table using the includes method, but it doesn't work, so please let me
- difference between executing puts in ruby method and executing puts when executing method
- ruby on rails undefined method `password_digest='
- ruby - update method product editing function flea app
- ruby - undefined method `deep_symbolize_keys' for "task:task":string error
- how to reuse instance variable in instance method of parent class in python?
- ruby - please tell me how to resolve the undefined method `id' for nil:nilclass error in rails
- ruby - about the range where the method cannot be used due to undef
- ruby - about behavior when "create instance of class" is passed to method argument
- about error of ruby select method
- ruby - undefined method `map' for nil: nilclass cannot be resolved
- conversion method when the response result is returned in csv (shift_jis) [ruby]
- ruby on rails - actionview::template::error (undefined method ~
- python 3x - how to define class with python for statement
- ruby, martingale method, simulation
- the method becomes nomethoderror due to the difference in the number of ruby arrays
- python 3x - typeerror: 'method' object is not subscriptable
- python - you may need to restart the kernel to use updated packages error
- xcode - pod install [!] no `podfile 'found in the project directory
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
- android studio - emulator: dsound: could not initialize about the error message directsoundcapture
- android studio - unresolved reference comes out in kotlin
- mysql startup failed [error] innodb: the innodb_system data file 'ibdata1' must be writable
- django - oserror: [winerror 123] the file name, directory name, or volume label syntax is incorrect : '<frozen importlib_boot
- python - importerror: cannot import name md5 error cannot be resolved