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.
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 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 3 years ago.
Improve this question
The LinkedList data structure tends to have different methods of implementation, even when coded in the same language.
Is there a fundamental specification for what a LinkedList must have somewhere? Sort of like the ECMA standard for JavaScript but for a LinkedList data structure (as well as others).
If you are asking for the methods a LinkedList data structure should expose in an OOP language, then it comes under the Collections interface of Java, or C# for example. At a very high level, it should expose methods like:
Create a new list of a given data type (i.e. template)
Add element/node to the list
Delete element/node from list
Get the next element/node of a given element/node so that you can interate over the list.
These are the bare minimum ones to operate with this data structure. Collections interface will declare more methods which are useful in high level languages.
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.
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 8 years ago.
Improve this question
while browsing for a contains method, I came across the following Q&A
contains-method-for-a-slice
It is said time and again in this Q&A that the method is really trivial to implement. What I don't understand is, if it were so easy to implement, and seeing how DRY is a popular software principle && and most modern languages implement said method , what sort of design reasoning could be involved behind the exclusion of such a simple method?
The triviality of the implementation depends on the scope of the implementation. It is trivial to implement when you know how to compare each value. Application code usually knows how to compare the types used in that application. But it is not trivial to implement in the general case for arbitrary types, and that is the situation for the language and standard library.
Figuring out if a slice contains a certain object is an O(n) operation where n is the length of the slice. This would not change if the language provided a function to do this. If your code relies on frequently checking if a slice contains a certain value, you should reevaluate your choice of data structures; a map is usually better in these kind of cases. Why should the standard library include functions that encourage you to use the wrong data structure for the task you have?
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 need a simple random English sentence generator. I need to populate it with my own words, but it needs to be capable of making longer sentences that at least follow the rules of English, even if they don't make sense.
I expect there are millions of them out there, so rather than re-inventing the wheel, I'm hoping you know of a source for one, or a resource that will give me enough information that I don't have to hunt down my rusty English skills.
You're looking for an implementation of markov chains for English sentences.
A quick Google search for "markov chain sentence generator" returned:
http://www.jwz.org/dadadodo/
http://code.google.com/p/lorem-ipsum-generator/
http://kartoffelsalad.googlecode.com/svn-history/r9/trunk/lib/markov.py
I know this is an old question, but as I found it via Google I think it's worth mentioning something like Context Free Grammars in addition to Markov Chains.
My understanding of Markov Chains is that they create the "next" item probabilistically only according to what the "current" item is. Perhaps I'm mistaken, but I don't see how this would ensure that the result would follow grammatical rules.
For instance, I usually see Markov Chains suggested as a way of creating "English sounding" words. If you create a Markov chain using letters from a dataset of English words, the output would be a word that sounds English, but it wouldn't necessarily be a real word. I believe the same would be true of sentences- you can generate something which may sound ok, but it may not be a grammatically correct sentence.
Context Free Grammars (or possibly also Regular Grammars?) might be a better candidate, since they generate sentences according to a defined ruleset. And it would be easy to populate it with your own words, as the original question requests. The downside is that you need to define these rules yourself, rather than relying on a dataset. It's been a long time since I've used a grammar to generate an English sentence, so I don't remember how hard it was to get good / varied responses.
You might be able to use/modify part of the CS Paper Generator.