On Sun, May 1, 2011 at 4:29 AM, Konstantin Ka <paranox3 / yahoo.de> wrote:
> Hey, thanks for the rapid response guys.
>
> @Oliver & Phillip Thanks for the pointer towards the source codes. (and
> book recommendations are also appreciated)

Yukihiro Matsumoto's book is the one you want. Everything else is just
peripheral.

>
> I guess that answers the part where the first methods / keywords come
> from to build upon in ruby. I mean I already guessed that part could
> only be answered by looking at the C code of Ruby itself.
>
> Now @Stu you seem to be onto what I was looking for with the other part
> of the original question.
>
> Even if the basic commands are embedded within the C of Ruby itself,
> aren't there already pre-defined (let's call them "2nd tier") methods
> based on Ruby code already within those superclasses?

In ruby version 1.9.x every object inherits from BasicObject.
BasicObject acts as a base class for everything.

Ruby doesn't really have fundamental data types. Everything is an
abstract data type which in ruby is closer to being defined as a
fundamental data structure. The abstraction becomes more obvious when
you can see the polymorphic design and method overloading in it's
classes.

For example what you may not realize is that your operators are also
methods. Ruby uses a fancy syntactic sugar to allow what looks like a
statement 1 + 3 which is really and expression 1.+(3)

+ is a method. In this case part of one of the numerical classes. It
becomes redefined for several other ruby fundamental data structures.
Example here is how + is used in ruby collections and strings. It's
just fluid to the programmer which is Matz' genius in polymorphic
object design.

[3.14, 42, 13] + [5,4,3,2,1]

"Hello, " + "Konstantin"

Looking at how the operator methods are used in collections and
strings may help you conceptually with ruby.

>
> Like, assume for a moment that the rand method you defined as an example
> above really was an embedded function in Ruby that I didn't have to
> define before I can use it. (I assume it isn't, right?)
>
> Would there be any way that Ruby-Newbie-mme could come along, have a
> look inside the preprogrammed String class and see written there in Ruby
> language
>
>>   
>>  
>>  
>
> ... because that would enable me to go "Oh, sure! It splits the string,
> shuffles it, and joins it back together. Sounds easy!" even if, to find
> out what some of those "1st tier" methods like .split actually do, I'd
> have to go look at the C code.

There is a project called pry which allows you to cd to the object and
type ls and look at it's state. But what it sounds like your looking
for is a more computer science concept of understanding data
structures and algorithms.

understanding these concepts most definitely will allow you to become
a better programmer in general but you need to consider that ruby is a
high level language. Data structures and algorithms tend to be best
grokked on low level languages or on paper or whiteboard. Do a google
for bubble sort. Do it by hand on paper. See if you can implement it
yourself with languages that provide larger datasets like ruby and
languages that provide a bare minimum set of function like C. mind you
C is not a beginners programming language as by design it doesn't
protect the programmer from overflowing the stack. It does allow
control of mapping memory though which makes it a great language to
understand how memory works in computers.

If you do go this route you'll begin to notice some easter eggs in
ruby (i.e. Queue, Struct push, pop). The difference is that in C you'd
actually implement push and pop yourself.

>
>
> Now Stu, the part where you explained:
>> the eval family of methods for
>> meta-programming which include:
>>
>> >>  
>> > [: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]
>>
>> ...
>
> I didn't understand a word of that... which probably means you are
> somewhat right that I might be looking for that :-)
>
> What would .grep(/eval/) do? What is an eval family? That returned array
> of symbols ... are those further methods?
>
> Thanks,
>
> -Konstantin

you made reference to redefining def which is possible but don't do that =)
I took that as you where interested in ruby's meta programming
facility. eval is short for evaluate expression. It's lineage comes
the UNIX command line program of the same name. I'll explain in a
second but first want to get the grep question out of the way.

grep is a search utility which also comes from the UNIX command set as
one of it's earliest programs. The UNIX philosophy initially was to
create tools that do one thing and do them well. If the program didn't
exist and couldn't be created with the set of tools already on the
system drop down to C and make a new tool. The programs would be
executed and output could stream to the input of another program via
system call known as pipe. These 'batch' scripts could be glued
together through a simple glue language called sh. sh was also the
shell and was the first to incorporate a repl and programming language
in one interface. A simple contrived example would be this

$  printf "Hello \nworld" | grep w

on the command line this would print the word 'world' because it
searched for anything that contained the letter 'w'.

Ruby allows me to grep strings and collections. ruby's object
hierarchy is exposed with the methods method which can be grepped as
so:

>> BasicObject.methods.grep(/methods/)
 => [:instance_methods, :public_instance_methods,
:protected_instance_methods, :private_instance_methods, :methods,
:singleton_methods, :protected_methods, :private_methods,
:public_methods]

grep's ancestor is the UNIX line editor ed. It's strange name comes
from g/re/p which is a construct in ed for global / regular expression
/ print. You can almost follow the lineage in how some programming
languages work from the history of these programs.

ed -> grep -> sed -> awk ------> perl -> ruby

Ruby borrowed many ideas from perl which was the first to break UNIX
design philosophy of many tools which could be programmed in a modular
fashion vs a monolithic tool that does everything.

Either way knowing something about the unix toolset will help you
understand many things in ruby. Ruby is a very unix-centric language.
If your interested in seeing what it's all about look at FreeBSD
(http://www.freebsd.org/)

A simple explanation on how eval works is to see the code below:

def generate_method( name)
  m = <<EOF
  def #{name}_say
    puts "#{name} is grokking the Ruby Programming Language"
  end
EOF
  eval m
end

Here I am using a heredoc which is really just a multi-line string
with interpolation and set in the variable m. when eval runs on m a
new method is created. Look at my irb output:

>> generate_method "stu"
 => nil
>> stu_say
stu is grokking the Ruby Programming Language
 => nil
>> generate_method "konstantin"
 => nil
>> konstantin_say
konstantin is grokking the Ruby Programming Language

Though this example is overly contrived you can see where it may be
useful for generating boilerplate functionality on the fly.

Hope some of this information is useful to you.

~Stu