Use case: The user (test developer) wants to create his own methods with his own formulas (body).
He is not allowed to change anything else, only to add (or change) methods in a (his) specific class.
- it's for getting comfortable with Ruby, seeing where the limits are...
Seems there is not much security, if allowing "foreign" code...
thanks
Opti
···
On 2017-01-30 16:46, Robert Klemme wrote:
On Mon, Jan 30, 2017 at 2:27 PM, Die Optimisten > <inform@die-optimisten.net> wrote:
I want to define methods at runtime:
class A
def initialize(name, arg)
define_method(name, {puts arg*2})
end
x=A.new(my_print, arg)
y=A.new(other_print, arg)
So it later can be used:
x.my_print(333)
y.other_print(444)
Use case: The user (test developer) wants to create his own methods with his
own formulas (body).
Why should they use define_method for that? They can just use def as usual.
He is not allowed to change anything else, only to add (or change) methods
in a (his) specific class.
You cannot easily enforce that so "allowed" is just a social
convention. But in that case you can also establish a convention which
is less awkward. Note also that your restriction is not really a
restriction as a lot can be done in a method body. So why bother?
- it's for getting comfortable with Ruby, seeing where the limits are...
Seems there is not much security, if allowing "foreign" code...
It is as secure as using eval on arbitrary strings in the shell.
Regards
robert
···
On Mon, Jan 30, 2017 at 8:37 PM, Die Optimisten <inform@die-optimisten.net> wrote:
Hi Robert,
therefore he's getting his own interface, which restricts him to his class only.
So the need of defining methods.
Social restrictions are usually not enough?!
Question is, if there is another way archiving that...
Opti
···
On 2017-01-31 08:15, Robert Klemme wrote:
On Mon, Jan 30, 2017 at 8:37 PM, Die Optimisten > <inform@die-optimisten.net> wrote:
Use case: The user (test developer) wants to create his own methods with his
own formulas (body).
Why should they use define_method for that? They can just use def as usual.
He is not allowed to change anything else, only to add (or change) methods
in a (his) specific class.
You cannot easily enforce that so "allowed" is just a social
convention. But in that case you can also establish a convention which
is less awkward. Note also that your restriction is not really a
restriction as a lot can be done in a method body. So why bother?
- it's for getting comfortable with Ruby, seeing where the limits are...
Seems there is not much security, if allowing "foreign" code...
It is as secure as using eval on arbitrary strings in the shell.
therefore he's getting his own interface, which restricts him to his class
only.
I am curious to learn how you would do that. We can even turn that
into a challenge: you post your restriction and the community comes up
with the simplest way to break it. Golfing at its best...
So the need of defining methods.
Social restrictions are usually not enough?!
But it's all you can get in absence of technical means - and they can
be quite powerful, too.
Cheers
robert
···
On Tue, Jan 31, 2017 at 11:29 AM, Die Optimisten <inform@die-optimisten.net> wrote:
Hello,
Just an idea to (try to) restrict to a class:
if it is surrounded with class X ... end
then no other classes SHOULD be possible to be modified.... [ please correct my English]
Maybe it's not possible to have security in Ruby...
- I'll get back to your challenge, if I have more wisdom
Opti
···
On 2017-01-31 17:55, Robert Klemme wrote:
On Tue, Jan 31, 2017 at 11:29 AM, Die Optimisten > <inform@die-optimisten.net> wrote:
therefore he's getting his own interface, which restricts him to his class
only.
I am curious to learn how you would do that. We can even turn that
into a challenge: you post your restriction and the community comes up
with the simplest way to break it. Golfing at its best...
So the need of defining methods.
Social restrictions are usually not enough?!
But it's all you can get in absence of technical means - and they can
be quite powerful, too.
therefore he's getting his own interface, which restricts him to his
class
only.
I am curious to learn how you would do that. We can even turn that
into a challenge: you post your restriction and the community comes up
with the simplest way to break it. Golfing at its best...
Just an idea to (try to) restrict to a class:
if it is surrounded with class X ... end
then no other classes SHOULD be possible to be modified.... [ please correct
my English]
Easy:
$ ruby <<CODE
class X
class ::Foo
end
end
p X
p Foo
p X::Foo
CODE
X
Foo
-:7: warning: toplevel constant Foo referenced by X::Foo
Foo
This proves that you can easily access and create classes in any scope
from inside a class scope.
- I'll get back to your challenge, if I have more wisdom
Don't wait too long.
Cheers
robert
···
On Tue, Jan 31, 2017 at 7:54 PM, Die Optimisten <inform@die-optimisten.net> wrote:
On 2017-01-31 17:55, Robert Klemme wrote:
On Tue, Jan 31, 2017 at 11:29 AM, Die Optimisten >> <inform@die-optimisten.net> wrote: