Creating methods at runtime

Hi

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)

Problem is that
     1) arg can't be given like that
     2) it substitutes arg at the initilazation with the value, I want the var as placeholder.

Can I do alias create initialize
  or is it better to alias create new ?
  Whats the difference?

Thank you!
Opti

Why do you want to do that? What is the use case?

Cheers

robert

···

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
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)

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

Do you really want to create objects with differently
named singleton methods that actually do the same thing?

Why?

Regards,
Marcus

···

Am 30.01.2017 um 14:27 schrieb Die Optimisten:

I want to define methods at runtime:

class A
def initialize(name, arg)
     define_method(name, {puts arg*2})
end
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)

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Hi,

Problem is that
    1) arg can't be given like that
    2) it substitutes arg at the initilazation with the value, I want
the var as placeholder.

The reason is that you specify a block without arg.

class A
   def initialize(name)
     define_singleton_method(name) { |arg| puts arg * 2 }
   end
end

x = A.new(:my_print)
x.my_print(111)
y = A.new(:other_print)
y.other_print(222)

This works but you can define singleton method when you need it.
why don't use it?

irb(main):016:0> class Foo;end
=> nil
irb(main):017:0> obj = Foo.new
=> #<Foo:0x3db57d8>
irb(main):018:0> def obj.my_print(arg)
irb(main):019:1> puts arg * 2
irb(main):020:1> end
=> :my_print
irb(main):021:0> obj.my_print(123)
246
=> nil

I strongly recommend you read "Metaprogramming Ruby2".

Regards,
Toshi

···

On 2017/01/30 22:27, Die Optimisten wrote:

Hi

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)

Why do you want to do that? What is the use case?

Cheers

robert

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:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

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.

Regards

robert

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:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

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 :slight_smile:

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.

Cheers

robert

or in any other language, especially when you eval user code...

Regards,
Marcus

···

Am 31.01.2017 um 19:54 schrieb Die Optimisten:

Maybe it's not possible to have security in Ruby...

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

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 :slight_smile:

Don't wait too long. :wink:

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:

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/