Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
The term metaprogamming confuses me. Words like "programs that write programs" isn't helping at all, and I could not find good examples. I only have a vague idea that it adds methods to class definitions. But many other languages can do that, e.g., Objective-C has Method swizzling (by changing Objective-C Runtime).
Why does Ruby emphasize metaprogramming so much? Can someone give me some examples of metaprogamming in Ruby? What is it used for? What kind of problems does it solve?
You should look at some resources on method_missing. Here is a good example of using it to make dynamic methods. You can also see some of the shortfalls and things to remember. Personally, I seldom use metaprogramming for my day in day out work. A good reason for using it might be to create an interface or a DSL for a library. You may also be better served by looking at the theme of creating interfaces in general, and you will learn things about metaprogramming along the way.
Edit: I didn't answer all of the questions.
I can't speak for everyone in the ruby community, but it is quite cool in principal, kind of scary in practice, queue up the quote from Peter Parker's uncle. Pretty much existing class is modifiable at anytime in ruby. Monkeypatching is another dangerous thing you can do. Essentially, ruby classes aren't final. You can open them back up and write to them. Think about in javascript how you can write to an Array class's prototype to create more functions directly on an array.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 1 year ago.
Improve this question
I'm refactoring old code and I want to find all functions that bigger than N lines to refactor it. It would be cool if there was the same tool for finding big classes.
The project has a lot of files so it is not so easy to find large methods manually.
I couldn't find this tool on the Internet, so, maybe you guys know a tool that can help me?
Thanks in advance!
You can find IDE plugins and external tools which will compute cyclomatic complexities of each of your methods to achieve your needs. You can find some tools in this subject
In the PVS-Studio analyzer there is the V553 diagnostic that reports that the size of the function or class exceeds 2000 lines. However, it's not the best idea to rely on the function size. It is because in addition to the length one should also take the function complexity into account. In this case, search for functions with large Cyclomatic complexity may help. For this, there is another V2008 diagnostic in PVS-Studio (note that it's disabled by default).
The tool NDepend can help finding large and complex classes and methods. There are several default rules for that:
Avoid types too big
Avoid methods too big, too complex
Avoid making large methods even larger
Avoid making complex methods even more complex
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
As the title stated, what are the pros and cons of Ruby meta-programming?
So far I know the advantages of Ruby meta-programming give us quite flexible ways to write our code using code that writes code. But what are the disadvantages? Maintainability issues?
I feel that meta-programming helps to scale an app also.
Can anyone can give more details about this?
Almost everything I know about metaprogramming in Ruby comes from "Metaprogramming Ruby: Program Like the Ruby Pros". It's a great read, and I suggest it to everyone who want to understand metaprogramming.
I'd say the main advantage is to have incredibly flexible code, which can adapt swiftly to any changes one could imagine. Things like send, respond_to? or define_method really give you the possibility of writing beautiful code.
On the other hand, I'd say that the main disadvantage is to make code harder to read and harder to debug, since most of the times the code isn't there yet! It will be generated at runtime, so you can't actually read it when you debug it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I was trying to find out how to create Ranges from custom objects in Ruby. I am very new to Ruby and I found the documentation on Range not to be helpful.
The error I was receiving when trying to create a Range from my objects was simply "Bad Value For A Range".
I eventually figured out after far to long, that in order to create a range from my object, I must define the "succ" and "<=>" functions.
My question is this. Is there a good resource that would have told me that I needed to define the above 2 functions in order to use my object in a range? I'd like to avoid problems like this in the future.
Sorry for the unconventional question. Thank you for your time.
The Pickaxe Book (AKA "Programming Ruby") has this to say about Range:
So far we've shown ranges of numbers and strings. However, as you'd expect from an object-oriented language, Ruby can create ranges based on objects that you define. The only constraints are that the objects must respond to succ by returning the next object in sequence and the objects must be comparable using <=>, the general comparison operator.
Emphasis mine. You have to be careful though, the Pickaxe that you'll find online is rather old and sometimes it doesn't agree with the current state of Ruby. There is an updated version for Ruby 1.9 but I don't think that one is freely available online so you'd have to buy a copy.
I usually end up digging through the Ruby source to figure out a lot of these things. That applies doubly so to Rails.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
This may be an unusual question, but are there any resources people have come across that list all generic or common functions/keywords used in most programming languages? Examples may include "var", "abs", "class", "int" and so on. I'm creating a syntax highlighter and was wondering if anyone any of these resources they would like to share. I'm aware that specific languages have their own appendix references, and I've used this to construct PHP syntax, if anyone has any of these references I'd appreciate if you shared them.
You will probably have to google for specific languages and combine the results yourself.
Maybe viewing the source of some open-source syntax highlighting tools will be helpful.
Also this site is pretty nice for comparison:
http://www.reservedwordsearch.com/
Any introductory book on a programming language will have a keyword reference table for that language. I imagine that any commonly available implementation today will also offer a similar table in electronic form.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
Where should I start to write a custom language for PLT? Is there any information on the net (or maybe information in the manual I'm overseeing?) Or are the existing language packs the best reference for such a task?
Thank you in advance!
The book - Programming Languages: Application and Interpretation might be a good place to start, especially chapter XII "Domain-Specific Languages and Metaprogramming". It uses PLT Scheme for the code samples, and the entire book is a worthy read.
This may be no longer relevant, but worth a mention. While PLAI is a good read, it is intended to be a general textbook on programming languages. If you want to get some specific details about creating new languages in PLT, then you are likely to be interested in a tutorial that I have given in DEFUN09 about this. I didn't make this public yet, since there is still some more work to do to get it in shape, but it is likely to be a useful resource if you're interested in this. (I will eventually turn it into a guide that will be included with the PLT documentation.) If you're interested, you can email me -- I'll send you a copy and I'll also be happy to get any feedback.