I asked chat.openai.com if there was a better way to implement dynamically defined classes in this module.
module Hive
def self.api_classes
@api_classes ||= {}
end
def self.const_missing(const)
super unless const.to_s.end_with? 'Api'
api = api_classes[const]
api ||= Api.clone(freeze: false) rescue Api.clone
api.api_name = const
api_classes[const] = api
end
end
The reply I got from AI:
It looks like the code you posted is defining a method named
const_missing
that is intended to be used as a callback when Ruby encounters a reference to a constant that hasn't been defined. In this method, the code checks to see if the constant name ends with "Api", and if it does, it attempts to clone anApi
class and save it in a hash.One possible way to improve this code would be to move the logic for defining a new
Api
class into its own method, and then call that method fromconst_missing
instead of duplicating the code. This would make the code easier to read and maintain. For example:
module Hive def self.api_classes @api_classes ||= {} end def self.const_missing(const) super unless const.to_s.end_with? 'Api' define_api_class(const) end def self.define_api_class(const) api = api_classes[const] api ||= Api.clone(freeze: false) rescue Api.clone api.api_name = const api_classes[const] = api end end
Another possible improvement would be to add error handling to the code in case
Api.clone
raises an error. For example, you could add arescue
clause to catch any errors and log them, or you could add a check to verify thatApi.clone
returned a validApi
class before storing it in the hash.Overall, the exact improvements you make to this code will depend on your specific use case and requirements. I would recommend considering the specific needs of your application and making changes accordingly.
This is a very competent response. If I were interviewing this person, I might hire them.
The AI doesn’t speculate about certain deeper concepts. There’s a reason I call Api.clone(freeze: false) rescue Api.clone
. Is a package designer, I want this code to be compatible with Ruby 2.6 through 3.x.
I also asked if there is a better way to cache these cloned classes so I can avoid using a module class variable.
One possible way to avoid using a module class variable to cache the cloned classes in the code you posted would be to use a thread-local variable instead. A thread-local variable is a variable that is local to the current thread, which means that each thread can have its own value for the variable without affecting the values in other threads.
To use a thread-local variable in this case, you could modify the code as follows:
module Hive def self.const_missing(const) super unless const.to_s.end_with? 'Api' api_classes[const] ||= begin api = Api.clone(freeze: false) rescue Api.clone api.api_name = const api end end def self.api_classes Thread.current[:api_classes] ||= {} end end
In this updated code, the api_classes method uses
Thread.current[:api_classes]
to store the hash of cloned classes. This means that each thread will have its own separate hash of cloned classes, and the values in one thread's hash won't affect the values in other threads' hashes.Using a thread-local variable in this way can be a good alternative to using a module class variable, since it avoids the potential for conflicts and race conditions that can arise when using class variables. However, it's important to note that thread-local variables can also have their own set of drawbacks and limitations, so you should carefully consider your specific use case and requirements before deciding which approach is best for your application.
That’s a great idea. I might even integrate these suggestions. I’m not too proud to accept them from an AI.
very good, AI-driven Development might finally close the gap between needs and capabilities in the IT-Dev Market.
It might make pull request approvals go from "LGTM" to "Hang on a second."
Wasn't it on your Twitter where I did watch an AI translate source code between different languages? Endless hours of people sitting in front of a screen reading API Docs and searching for green checks on StackOverflow can be replaced with an intelligent coding buddy ai.
I just tried it myself, absolutely crazy!
I'm going to ask it to improves some sql code :)
That's what I'm going to do next. Be sure to tell it the metadata and relative number of rows in the tables involved.
Like the name of the table/s, columns?
Sure and more importantly, the datatypes and any related stored procedures.
But I'm thinking, if you use a JOIN clause, but the table you're joining has fewer records than the table in the FROM clause, the AI might tell you to switch them. You could tell it that the FROM table has fewer records so it doesn't make that particular recommendation.
Ok, thanks .... obviously some work will be needed, but if we manage to make it work, so it can review code and make recomendations ....even some that are not a 100% on spot, but you can extrapolate from there, its still huge
uuh juicy
you'll get some trojan horse for free
Have you tried Copilot?
Nope.
It is worth playing around with, I really like using it. It is really impressive at times, especially when it makes comments you don't expect.
Rats.
It's worth giving a go, it's the best I've seen. It was really nice when it was completely free for beta testing though. $10/m, it's now really only worth it if you code regularlly. It is impressive at times.
Yeah, I'll check it out with IntellJ IntelliJ IDEA.
Fixed typo.
Seen your post on that in the past, 10$/m is not too insane. What lang are you using it for? Any?
Dear @inertia,
Your support for the current HiveSQL proposal (#138) is much appreciated but the proposal will expire soon!
May I ask you to review and support the new proposal so HiveSQL can stay free to use for the community?
You can support the new proposal (#247) on Peakd, Ecency,
Thank you!
Dear @inertia,
Your support for the current HiveBuzz proposal (#199) is much appreciated but the proposal will expire soon!
May we ask you to review and support the new proposal so our team can continue its work?
You can support the new proposal (#248) on Peakd, Ecency,
Thank you!
Dear @inertia,
Your support for the current HiveBuzz proposal (#199) is much appreciated but the proposal will expire soon!
May we ask you to review and support the new proposal so our team can continue its work?
You can support the new proposal (#248) on Peakd, Ecency,
Thank you!
Thank you so much for your support of my @v4vapp proposals in the past, my previous one expired this week.
I'd be really happy if you would continue supporting my work by voting on this proposal for the next 6 months:
Additionally you can also help this work with a vote for Brianoflondon's Witness using KeyChain or HiveSigner
If you have used v4v.app I'd really like to hear your feedback, and if you haven't I'd be happy to hear why or whether there are other things you want it to do.
My previous @v4vapp proposal has expired. I have a new one which is running but unfunded right now. I'm still running @v4vapp and all my other services but I may have to increase the 0.8% fee to 2.0% if I continue to be unfunded.
Please consider asking your friends to vote for prop #244 or consider unvoting the HBD stabilizer explained below.
For understandable reasons in the current crypto climate it is harder to get funded by the DHF, I accept this so I'm asking a wider audience for help again. I will also redraft this proposal with more details (coming soon). I'm also looking for other funding sources.
Additionally you can also help with a vote for Brianoflondon's Witness using KeyChain or HiveSigner
If you have used v4v.app I'd really like to hear your feedback, and if you haven't I'd be happy to hear why or whether there are other things you want it to do.