metaprogramming

Groovy meta-programming - adding static methods to Object.metaClass

[亡魂溺海] 提交于 2020-01-01 09:27:10
问题 I've encountered a Groovy meta-programming problem which I'm unable to solve. When adding the static method foo() to the class FooBar, then FooBar.foo() works as expected: FooBar.metaClass.static.foo = { println "hello" } FooBar.foo() However, I instead add the same static method foo() to the class Object, then FooBar.foo() fails with an MissingMethodException: Object.metaClass.static.foo = { println "hello" } FooBar.foo() // groovy.lang.MissingMethodException: // No signature of method:

dynamically create a class without a namespace

∥☆過路亽.° 提交于 2020-01-01 07:11:25
问题 I am trying to dynamically create a class using the eval method. It is working fine except for one small problem. As my code shows I am creating the Browser class inside the BrowserFactory class. When I do this the Browser class has an added namespace of BrowserFactory. Is there anyway to evaluate the Browser class from a string without the BrowserFactory namespace being attached? class BrowserFactory def self.create_browser(browser) super_class = nil case browser when 'IE' require 'watir'

Output the source of a ruby method

血红的双手。 提交于 2020-01-01 05:50:10
问题 Say I make a class with a method in it. class A def test puts 'test' end end I want to know what goes on inside of test . I want to literally output: def test puts 'test' end Is there any way to output the source of a method in a string? 回答1: You can use Pry to view methods # myfile.rb require 'pry' class A def test return 'test' end end puts Pry::Method(A.new.method(:test)).source #(1) # or as suggested in the comments puts Pry::Method.from_str("A#test").source #(2) # uses less cpu cycles

loading/unloading/updating class in ruby

旧街凉风 提交于 2020-01-01 05:37:05
问题 I did a little experiments with Ruby class dynamic loading/unloading/updating as implementing plugins infrastructure. I found a few points: If loading new version of same class without first unloading it, the new one essentially 'top' or 'merge' with previous version. All existent objects created with previous version would get their class definition 'updated'. Unloading a class does not affect existent objects created with this class. Existent objects stay with whatever version just unloaded

How to make super() work by manually filling the __class__ cell?

倾然丶 夕夏残阳落幕 提交于 2019-12-31 21:35:08
问题 In Python 3 one can use super() instead of super(MyClass, self) , but this only works in methods that were defined inside the class. As described in Michele Simionato's article the following example does not work: def __init__(self): print('calling __init__') super().__init__() class C(object): __init__ = __init__ if __name__ == '__main__': c = C() It fails because super() looks for a __class__ cell, which is not defined in this case. Is it possible to set this cell manually after the

Typed metaprogramming languages [closed]

你说的曾经没有我的故事 提交于 2019-12-31 20:11:10
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I want to do some metaprogramming in a statically typed language, where both my programs and my meta-programs will be typed. I mean

Typed metaprogramming languages [closed]

冷暖自知 提交于 2019-12-31 20:09:43
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I want to do some metaprogramming in a statically typed language, where both my programs and my meta-programs will be typed. I mean

r data.table functional programming / metaprogramming / computing on the language

*爱你&永不变心* 提交于 2019-12-31 16:47:47
问题 I am exploring different ways to wrap an aggregation function (but really it could be any type of function) using data.table (one dplyr example is also provided) and was wondering on best practices for functional programming / metaprogramming with respect to performance (does the implementation matter with respect to potential optimization that data.table may apply) readability (is there a commonly agreed standard e.g. in most packages utilizing data.table) ease of generalization (are there

What do “reify” and “reification” mean in the context of (functional?) programming?

这一生的挚爱 提交于 2019-12-31 08:33:27
问题 I read this term a lot in blogs about haskell and functional programming (specially in sigfpe's blog) but I don't have a clue about what it means. I get away with not knowing it most of the times, but I probably would have understood the texts a lot better if I knew. Google didn't help me. I get lost in the technical stuff. Also the non-technical meaning of the world ("turning the abstract concrete") doesn't help me understand what it practically means to reify something in code. I'm kinda

What do “reify” and “reification” mean in the context of (functional?) programming?

a 夏天 提交于 2019-12-31 08:32:50
问题 I read this term a lot in blogs about haskell and functional programming (specially in sigfpe's blog) but I don't have a clue about what it means. I get away with not knowing it most of the times, but I probably would have understood the texts a lot better if I knew. Google didn't help me. I get lost in the technical stuff. Also the non-technical meaning of the world ("turning the abstract concrete") doesn't help me understand what it practically means to reify something in code. I'm kinda