This falls in the realm of Metaprogramming, which can be notoriously slow. Avoid it unless you know for a fact you need it. In fact, if you're a newer programmer, avoid it altogether.
A grenade launcher is powerful and awesome, yes, but dang can you blow your foot off fast.
This falls in the realm of Metaprogramming, which can be notoriously slow. Avoid it unless you know for a fact you need it. In fact, if you're a newer programmer, avoid it altogether.
A grenade launcher is powerful and awesome, yes, but dang can you blow your foot off fast.
They're not, but at the same time the chances you actually need it are slim to none. It just makes the code substantially harder to follow, often times for little to no reason.
# Not really any noticeable performance lossdefine_methodname) do |*args| args.each(&rint)end# Considerable performance loss due to the use of proc.proc = Proc.new {|*args| args.each(&rint) }senddefine_method,:name, proc)Also, imo - if you can justify it, use it.
I mean...
[*1..12].each do |digit| proc = Proc.new { trigger?("VK_F#{digit}".to_sym) } senddefine_method, "f#{digit}?", proc) end^ There was no way in hell I was writing that out manually. For this, the amount of time the methods may* be used (*could be never) is very small, so any potential performance lost would be minimal.
Edit:
Just done some simple benchmark comparison and uhh... Seems my previous statement was sightly inaccurate. So I shall change it..
* There is more chance of you incurring unnecessary overhead by using a proc. The performance difference in minimal when used sparingly/effectively. However, compared to a normal method definition, it is undoubtedly slower.
(test results in spoiler)
This is the test results of the example (below result) of using define_method.
Code:
Calculating ------------------------------------- A 46.898k i/100ms B 44.005k i/100ms C 50.202k i/100ms------------------------------------------------- A 1.459M (± 5.6%) i/s - 14.538M B 1.456M (± 6.1%) i/s - 14.522M C 1.776M (± 6.8%) i/s - 17.671MComparison: C: 1776229.6 i/s A: 1458942.9 i/s - 1.22x slower B: 1455862.6 i/s - 1.22x slower
Code:
module TEST class << self define_method(:mthoA) do |*args| args.count end proc = Proc.new { |*args| args.count } send(:define_method,:mthoB, proc) def mthoC(*args) args.count end endend
[*1..12].each do |digit| proc = Proc.new { trigger?("VK_F#{digit}".to_sym) } senddefine_method, "f#{digit}?", proc) end^ There was no way in hell I was writing that out manually. For this, the amount of time the methods may* be used (*could be never) is very small, so any potential performance lost would be minimal.
Metaprogramming for the sake of metaprogramming makes a mess. You could make it f(1) for all it really matters, but the point is that in my experience people who rely on metaprogramming make a large mess for little to no other reason than 'because it was there' or 'because it was shiny.'
Perhaps, but that is not quite the reason for my using it.
I already have methods such as trigger!VK_F1) that would perform the logic I assume your suggested 'f_trigger(1)' or 'f(1)' would do. I simply decided to add additional methods (for those who may want to use them) for quickly checking F key presses/triggers.
My reason for using meta-programming techniques to add the methods was simply because it meant I was able to quickly define 12 methods in 4 lines of code. also felt that code like this...
so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most.
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.