# Method prototypes like C++? // Compiling executables

**URL:** <https://www.ruby-forum.com/t/method-prototypes-like-c-compiling-executables/183668>\
**Category:** Ruby\
**Created:** [February 18, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/method-prototypes-like-c-compiling-executables/183668 "2010-02-18T14:50:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferdil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ec9cab/32.png) [@ferdil](https://www.ruby-forum.com/u/ferdil)\
**Post date:** [February 18, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/method-prototypes-like-c-compiling-executables/183668/1 "2010-02-18T14:50:24Z")

</div>

Hi, I’m switching from C++ to Ruby, I have a few questions.

1. How come there isn’t any “method prototype” feature like the function  
prototypes in C++? Like, in c++ I declared just the name of a funcion at  
the beginning of a block of code and then I could put the function  
anywhere in the program, so it could be called wherever, without  
worrying if it had been fully declared or not. Is this possible?

2. I find it awesome that you needn’t compile to view the results of  
your code; still, i would like to be able to compile my programs to an  
executable file for distribution. Is it possible?

3. I still don’t get how this whole “gems” thing work. In C++ I used to  
load libraries, it seems that in Ruby most of the libraries are loaded  
by themselves when needed. When exactly do I need to include other  
libraries?

---

<div class="post-metadata">

**Author:** ![ferdil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ec9cab/32.png) [@ferdil](https://www.ruby-forum.com/u/ferdil)\
**Post date:** [February 18, 2010, 2:56pm UTC](https://www.ruby-forum.com/t/method-prototypes-like-c-compiling-executables/183668/2 "2010-02-18T14:56:23Z")

</div>

Also, I forgot:

1. In C++ I used to use “labels” to redirect quickly to small chunks of  
code when making a whole function seemed useless; or when I wanted to  
reask a question if the user input was not recognised. I know their use  
is deprecated, In fact I was able to do just what i did before with  
methods and exception handling.  
Just for asking, there is no label-like feature in Ruby, isn’t it?

---

<div class="post-metadata">

**Author:** ![ferdil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ec9cab/32.png) [@ferdil](https://www.ruby-forum.com/u/ferdil)\
**Post date:** [February 18, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/method-prototypes-like-c-compiling-executables/183668/3 "2010-02-18T15:18:45Z")

</div>

On Feb 18, 2010, at 8:50 AM, Claudio F. wrote:

> Hi, I’m switching from C++ to Ruby, I have a few questions.
> 
> 1. How come there isn’t any “method prototype” feature like the  
> function  
> prototypes in C++? Like, in c++ I declared just the name of a  
> funcion at  
> the beginning of a block of code and then I could put the function  
> anywhere in the program, so it could be called wherever, without  
> worrying if it had been fully declared or not. Is this possible?

Sure just do it! Methods only need to exist when then are “called”  
and even then you can use the method\_missing hook to define them on  
the fly if you need to.

> 1. I find it awesome that you needn’t compile to view the results of  
> your code; still, i would like to be able to compile my programs to an  
> executable file for distribution. Is it possible?

Look at JRuby’s ability to create .class files for the JVM, but you  
might want to keep an open mind about whether you _really_ need to  
compile.

> 1. I still don’t get how this whole “gems” thing work. In C++ I used  
> to  
> load libraries, it seems that in Ruby most of the libraries are loaded  
> by themselves when needed. When exactly do I need to include other  
> libraries?

It’s a way to have multiple versions of libraries on your system.  
You’ll know you need more because something doesn’t exist. For  
example, there are some useful methods on Date that are only available  
if you “require ‘date’” in your code. (Not the best example because  
that’s not a gem, but a standard library, i.e., it comes as part of  
Ruby.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ferdil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ec9cab/32.png) [@ferdil](https://www.ruby-forum.com/u/ferdil)\
**Post date:** [February 18, 2010, 3:21pm UTC](https://www.ruby-forum.com/t/method-prototypes-like-c-compiling-executables/183668/4 "2010-02-18T15:21:05Z")

</div>

how to unsubscribe from this mailing list can any one please reply me.

thanks  
sandeep

---

<div class="post-metadata">

**Author:** ![ferdil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ec9cab/32.png) [@ferdil](https://www.ruby-forum.com/u/ferdil)\
**Post date:** [February 18, 2010, 6:42pm UTC](https://www.ruby-forum.com/t/method-prototypes-like-c-compiling-executables/183668/5 "2010-02-18T18:42:30Z")

</div>

On Thu, Feb 18, 2010 at 6:28 PM, Claudio F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> void function() {
> 
> def method  
> print (‘The method has been executed’)  
> end
> 
> it just prints me nothing; I think it has something to do with ruby’s  
> dynamic definitions.
> 
> Just how to achieve the same thing in ruby? (summoning a function that  
> is declared later in the code)

What Rob wanted to say is that you don’t need to declare a function  
(method). You just call it, and it has to have been defined before the  
call. But be aware that the code inside a method is not executed when  
it’s parsed, only when the method is called, so you can have this:

irb(main):001:0\> def caller  
irb(main):002:1\> method(“a”)  
irb(main):003:1\> end  
=\> nil  
irb(main):004:0\> def method(s)  
irb(main):005:1\> puts s  
irb(main):006:1\> end  
=\> nil  
irb(main):007:0\> caller  
a

You can have the definition of caller before the definition of method.  
What you can’t do is call caller before defining method.

Hope this clears up a bit,

Jesus.

---

<div class="post-metadata">

**Author:** ![ferdil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ec9cab/32.png) [@ferdil](https://www.ruby-forum.com/u/ferdil)\
**Post date:** [February 19, 2010, 1:05pm UTC](https://www.ruby-forum.com/t/method-prototypes-like-c-compiling-executables/183668/6 "2010-02-19T13:05:21Z")

</div>

2010/2/18 Claudio F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> void function() {  
> cout\<\<“The function has been executed”  
> }
> 
> And it would still print “The function has been executed”

You need that in C / C++ only because implementations are split to  
header and source files. We don’t do that in Ruby. Instead, we  
implement whatever functionality we need and place it in one or more  
.rb files. Those are then read by using “require”.

> it just prints me nothing; I think it has something to do with ruby’s  
> dynamic definitions.
> 
> Just how to achieve the same thing in ruby? (summoning a function that  
> is declared later in the code)

That’s not a problem as long as execution starts later:

def foo  
method  
end

def method  
puts ‘It works!’  
end

foo

This is typically the case if your required files contain declarations  
only.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![ferdil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ec9cab/32.png) [@ferdil](https://www.ruby-forum.com/u/ferdil)\
**Post date:** [February 18, 2010, 6:28pm UTC](https://www.ruby-forum.com/t/method-prototypes-like-c-compiling-executables/183668/7 "2010-02-18T18:28:47Z")

</div>

Rob B. wrote:

> Sure just do it! Methods only need to exist when then are “called”  
> and even then you can use the method\_missing hook to define them on  
> the fly if you need to.

Wait, I explain my problem.  
In C++ I would have been able to to this:

void function();  
function();  
void function() {  
cout\<\<“The function has been executed”  
}

And it would still print “The function has been executed”

In ruby instead if I do this:

def method; end

method

def method  
print (‘The method has been executed’)  
end

it just prints me nothing; I think it has something to do with ruby’s  
dynamic definitions.

Just how to achieve the same thing in ruby? (summoning a function that  
is declared later in the code)
