From: Stu Date: 2011-05-01T16:48:41+09:00 Subject: Re: Understanding Ruby Inside Out On Sun, May 1, 2011 at 1:15 AM, Konstantin Ka wrote: > What makes Ruby work? > > This is not a guide. It's a question. And I hope there is a Ruby crack > reading this who can answer. (Be kind. It's my first post. :-) > > I've been learning the basics of Ruby for a bit over a week now and > while I understand those basics I can't help but wonder how all of those > basic methods work inside ruby. > > In the past I also noticed that the best way to become good at anything > is to understand every detail of it (I had an education in medicine and > if you know what molecules and cells do if you throw them together, the > function and malfunctions of organs s just basic logic) > > That being said, since everything in Ruby is an Object and in one way or > another belongs to the Object-Class, I'd like to see the code of that. > > I want to see what basic methods do in code form. Even Kernel methods > like ... I don't know... .sort for example. > > Since Kernel is a subclass of Module and Module a subclass of Object, in > one of those 3 classes every (Kernel) method had to have been defined at > one point, right? > > So, is there any way I can see the class codes... or even let the code > be printed in irb via a method? Like "print sort.code" and it prints a > string with the original code inside, that would rock. (Well obviously > it would be looking for an array to sort first so that wouldn't work but > ... I hope you get what I mean) > > Especially interesting woud be that since every method seems to be > defined with other methods, where did the first methods come from? > > How would one define the method ".def"? > > Or maybe I am thinking in an entirely wrong direction? Especially since >> Class.superclass #=> Module >> Module.superclass #=> Object > # but >> Object.class #=> Class > > ... which I can't wrap my head around. > > > These questions seem fairly basic to me, but none of the beginner guides > I found even came close to explaining this. So please point me in the > right direction. > > Thank you, > > -Konstantin > > -- > Posted via http://www.ruby-forum.com/. > > Hello Konstantin The low level sort algorithm is written in C. if you need to look at it you will have to look at the source code. is there a specific reason you need to see sort? I'm sure it's a qsort or bubble sort algorithm. To define a method .def you can within your own classes. but what I believe your really looking for is the eval family of methods for meta-programming which include: >> Kernel.methods.grep(/eval/) => [:eval, :module_eval, :class_eval, :instance_eval] There is also the define_method which is cleaner and is the ruby way of dynamically generating methods. (i.e. with a block) define_method is a private method of the Module class >> Module.private_methods.grep(/define_m/) => [:define_method] also consider how easy it is to open up a class and extend it on it's own structure without eval: Class String def rand self.split('').shuffle.join end alias random rand end >> "Hello, Konstantin".rand => " ,oHlnKntstoanlie" >> "Hello, Konstantin".random => "onet a,tKinlonlHs" I reopened class String. created a method called rand. Since everything in ruby is a sender and receiver I sent the string "Hello, Konstantin" through a split() method which returns a array of chars. Then through the shuffle() method which acts like a driver providing the randomness to the array. then they are joined back into a string with the join() method which then returns the now modified string to the top level main calling object. I also used an alias to show that shortcuts and duplication of methods can me simplified. One thing that confuses people when they first start ruby is that there is an implicit return for all methods. The rule is the last expression is returned. I could have easily written return(self.split('').shuffle.join) if I needed to be more verbose. Another note that may help you conceptually is that everything in ruby has a binding. When you get to variables and scope remember that as it may help. ~Stu