How do I dynamically define a method as private?
Test.instance_eval { private :private_method }
Or, just run
private :private_method
from within the Test
class.
Dynamically create a public method in a private method
There are several problems with your code. The error you are getting has absolutely nothing to do with private
or public
at all. The error message says that the method create_method
cannot be found. There are two reasons for that:
- You are calling it before it is defined. You need to move the call to
create_method
after its definition. create_method
is defined as an instance method, i.e. for calling it on instances ofFoo
, but you are calling it onFoo
itself. You have to define it as a method somewhere inFoo
's class (i.e.Class
), one of its ancestors (e.g.Module
), orFoo
's singleton class.
I will define it as a singleton method of Foo
here, but if the method really is as generic as you have showed in your example, then it probably rather belongs in Module
instead.
class Foo
class << self
private
def create_method(name)
define_method(name) do
puts "HELL"
end
end
end
["bar", "baz"].each do |method|
create_method(method)
end
end
Foo.new.bar
# HELL
define_method: How to dynamically create methods with arguments
If I understand your question correctly, you want something like this:
class Product
class << self
[:name, :brand].each do |attribute|
define_method :"find_by_#{attribute}" do |value|
all.find {|prod| prod.public_send(attribute) == value }
end
end
end
end
(I'm assuming that the all
method returns an Enumerable.)
The above is more-or-less equivalent to defining two class methods like this:
class Product
def self.find_by_name(value)
all.find {|prod| prod.name == value }
end
def self.find_by_brand(value)
all.find {|prod| prod.brand == value }
end
end
Dynamically create private methods in JavaScript
There are no private properties on javascript objects, and I'd say that it's fine to have them just prefixed with an underscore.
However, you could try:
$.fn.wPluginTest.extend = function(funcs) {
var proto = PluginTest.proto,
priv = Object.create(proto); // a private namespace inheriting from proto
for (func in funcs) {
if (func.charAt(0) != '_')
PluginTest.prototype[func] = funcs[func].bind(priv);
else
priv[func.slice(1)] = funcs[func];
// ^^^^^^^^^ not sure, might be confusing
}
}
Yet, those functions will not have access to the PluginTest
instance they're called on - they're statically bound to the prototype objects - so it's probably a bad idea.
If any plugin needs real privateness, it should utilize variable scoping on its own.
How does dynamic binding work for private methods in base class in Java?
is the private sayHello from base class visible to the derived class?
Of course, no, because it has private
access modifier.
More about access modifiers:
Controlling Access to Members of a Class
or is it a redefinition?
As you can see in the accepted answer to this question:
What's the difference between redefining a method and overriding a method?
the term "redefinition" isn't commonly used. We can talk about "overriding" and "overloading", but in your case sayHello
from the Derived
class is a kind of new method and it's not an overloaded version of sayHello
from the Base
class.
And why does the call to the derived sayHello from the base pointer
does not work?
Simply because you try to call method that doesn't belong to the open class interface.
I mean, if it were public (in Base), then the sayHello from the
derived class would have been called.
Of course, it's an expected polymorphic behaviour. In this case, sayHello
from the Derived
class overrides sayHello
from the Base
class, so you can call sayHello
from the Derived
class via the reference to the the Base
class.
So, what I can not understand is that if it has to call the public
sayHello from the derived class, then why look at the access modifier
from the base class?
Because you use reference to the Base
class and there's no sayHello
method in the interface of the Base
class.
I found a good discussion here:
Overriding private methods in Java
May be also useful for you:
Overriding and Hiding Methods
Hope it'll help you.
How to dynamically define a class method which will refer to a local variable outside?
Class methods don't really exist in Ruby, they are just singleton methods of the class object. Singleton methods don't really exist, either, they are just ordinary instance methods of the object's singleton class.
Since you already know how to define instance methods (using Module#define_method
), you already know everything you need to know. You just need to call class_eval
on C
's singleton class instead of C
itself:
(class << C; self end).class_eval do
define_method(:a_class_method) do
puts var
end
end
Current versions of Ruby have a singleton_class
method to make that easier:
C.singleton_class.class_eval do
define_method(:a_class_method) do
puts var
end
end
But actually, current versions of Ruby also have Module#define_singleton_method
, so, in this particular case, that is unnecessary:
C.define_singleton_method(:a_class_method) do
puts var
end
js dynamically access private fields (properties/members)
I dont think you can access private fields dynamically. The proposal says:
There are no private computed property names:
#foo
is a private identifier, and#[foo]
is a syntax error.
Related Topics
What Do 'Def +@' and 'Def -@' Mean
Get Response Headers from Curb
Ubuntu 12.10 - Ruby Gem Rmagick Missing Dependency Issue
Installing Ruby Gem Less-Rails on Windows MAChine Using Therubyracer
How to Set Ca-Bundle Path for Openssl in Ruby
Rails:Runtimeerror - Can't Modify Frozen Array When Running Rspec in Rails
Using Activerecord Find_In_Batches Method for Deleting Large Data
Ruby Mailer Is Coming Up with an Eoferror
Remove Double Quotes from String
Devise Raises Error with Rails 4.2 Upgrade
How to Do "Late" String Interpolation in Ruby
Access Ruby Hash Using Dotted Path Key String
How to Require a Block in Ruby
Extract All Urls Inside a String in Ruby