From: "Jan E." Date: 2012-12-09T08:38:15+09:00 Subject: Re: if n belongs to set a and m belongs to set b repeat some steps, How? Thanks a lot. And I find it great that you really want to learn Ruby in depth. Many people just keep their habits from their previous language and merely change the syntax. This way they'll of course never experience the capabilities of Ruby or get to know new ideas. I think the key to "the Ruby way" is to forget the low-level procedural style we all know from languages like Java or C. When you want to do something with arrays in Java, you have to do it "by hand". You have to actually go through the elements and put them into the desired place. That's because Java arrays (and the derived classes) are basically limited to "assign a value to an index" and "get the length". Ruby, on the other hand, is much more expressive. It borrows many powerful features from functional languages like Haskell ("map", "select", "reduce", "take", "partition" etc.), and there are a lot of specialized methods. So whatever you want to do, there's probably a method or combination of methods for that. You rarely need to fumble with indices and move variables around. A good way of learning Ruby is to regularly read the API reference (http://www.ruby-doc.org). After a while, you'll know the methods and what you can do with them. You should also try to think in more abstract terms. Instead of "I'll have this loop here and that counter there", think about what you actually want to do like for example "I want to get the first n elements of an array". In the long run, you might also look into functional languages like the beautiful Haskell. It really teaches you a different way of thinking, and I got a lot of insights from it. Whatever you do, have fun. :-) Regarding your code: You can simplify the method a lot if you work with subarrays instead of passing the original array and the current indices around. So the methods should just be def merge_sort list ... end def merge left, right ... end The main thing that makes the code long and complicated is obviously the index arithmetic. But there's not really much you can do about it apart from completely getting rid of it. Since the counters depend on the order of the elements, you cannot use Ruby's counting methods ("upto", "each"), because those all count "statically" in a fixed pattern. By the way, method names in Ruby usually use underscores and not camelCase like in Java. And you don't need to initialize arrays with a certain length. Just start with an empty array: []. You should also use the keywords "and" and "or" instead of "&&" and "||". The latter have a high precendence and are rather used for technical stuff like setting a default value. The keywords are what you'd use for "if" statements and logical expressions. I've attached a modified version of your code. Attachments: http://www.ruby-forum.com/attachment/7944/merge_sort_update.txt -- Posted via http://www.ruby-forum.com/.