Ruby coding help

Hello All

I want to code a method within a ruby source code. Is there any syntax
to call that method(Which I coded) while writing a program in ruby. If
so can anyone share it?

Regards

Tridib

You can use the interactive editor gem

see here if I understand you correctly:

http://vimcasts.org/episodes/running-vim-within-irb/

~

Tridib B. wrote in post #980711:

Hello All

I want to code a method within a ruby source code. Is there any syntax
to call that method(Which I coded) while writing a program in ruby.

Certainly. What exactly are you trying to do?

You can run arbitrary code from a string using eval(), or from a file
using load(). This code can define methods.

Once a method is defined, then you can just call it in the usual way.

If you want to call a method with a name which is not known until
runtime, use send().

str = “module M; def self.foo; puts ‘whee!’; end; end”
=> “module M; def self.foo; puts ‘whee!’; end; end”

eval str
=> nil

M.foo
whee!
=> nil

a = “M”
=> “M”

b = “foo”
=> “foo”

Object.const_get(a).send(b)
whee!
=> nil

No. My query was… I am going to type in a method within some file of
Ruby… like gc.c and will call that method from Ruby Code… like

I will code a free method within source code and will call that function
from a ruby code while coding… So it is possible… if so what is
the syntax to call the method.

Regards

Tridib

hi Tridib,

Ruby methods : It provides a way to organize code and promote re-use.
Rather than create long sections of Ruby code, the code is instead
organized into logical groups that can be called when needed and re-used
without having to re-write the same code over and over. Methods are
simple to use, in fact you only need to do two things with a method,
declare it and call it.

How to declaring and Calling a Ruby Method

The syntax of a Ruby method is as follows:

def name( arg1, arg2, arg3, … )
… ruby code …
return value
end

The name specifies how we will refer to this method when we call it. The
args specify values that are passed through to the method to be
processed. The ruby code section represents the body of the function
that performs the processing. The optional return statement allows a
value to be returned to the section of code which called the method.

Any suggestion to my previous query.

Tridib

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs