Ruby Programming

Hello

I am new in Ruby. Can any one post a simple Program which requires
memory allocation.

Regards

Tridib B.

On 11/18/2010 5:30 PM, Tridib B. wrote:

Hello

I am new in Ruby. Can any one post a simple Program which requires
memory allocation.

Creating any object requires memory allocation of some sort. Are you
actually asking about performing memory allocation as part of a C-based
Ruby extension?

-Jeremy

BEGIN CODE:
#/usr/bin/env ruby
myvar = 1
END OF CODE

Easy huh?

On Thu, Nov 18, 2010 at 3:30 PM, Tridib B.
[email protected] wrote:

Hello

I am new in Ruby. Can any one post a simple Program which requires
memory allocation.


Aaron T.
http://synfin.net/ Twitter: @synfinatic
http://tcpreplay.synfin.net/ - Pcap editing and replay tools for Unix &
Windows
Those who would give up essential Liberty, to purchase a little
temporary
Safety, deserve neither Liberty nor Safety.
– Benjamin Franklin
“carpe diem quam minimum credula postero”

On Fri, 19 Nov 2010 10:30:30 +0900, Tridib B.
[email protected] wrote:

which i tried to print out.?

Regards

Tridib

Sure. There’s also the GC_NOTIFY and GC_DEBUG defines in gc.c and
GC::Profiler which you can call from Ruby. As people have already
pointed
out any Ruby program will require memory allocation and hence GC.

Hello

I need to do this…

The Gc is called at the runtime in Ruby. So I try to code a program
which requires memory allocation and Garbage collector.

Now the scenario is such that— In the gc.c file if I write some print
statement within any function.So when the gc comes for working, Will it
show me the message
which i tried to print out.?

Regards

Tridib

I am trying for the code

#/usr/bin/env ruby
myvar = 1

And also made some print statements in gc.c file functions (malloc,
calloc,free, gc_diable, gc_enable) etc.

But when I run the program I am not able to see the print statement.

Secondly, can any one say me that when the gc is called which is the
main function that starts running.

Regards

Tridib

I have done a coding
The code:-

Class names must be capitalized. Technically, it’s a constant.

class Fred

The initialize method is the constructor. The @val is

an object value.

def initialize(v)
@val = v
end

Set it and get it.

def set(v)
@val = v
end

def get
return @val
end
end

Objects are created by the new method of the class object.

a = Fred.new(10)
b = Fred.new(22)

print "A: “, a.get, " “, b.get,”\n”;
b.set(34)
print "B: “, a.get, " “, b.get,”\n”;

Ruby classes are always unfinished works. This does not

re-define Fred, it adds more stuff to it.

class Fred
def inc
@val += 1
end
end

a.inc
b.inc
print "C: “, a.get, " “, b.get,”\n”;

Objects may have methods all to themselves.

def b.dec
@val -= 1
end

begin
b.dec
a.dec
rescue StandardError => msg
print "Error: ", msg, “\n”
end

print "D: “, a.get, " “, b.get,”\n”;


Is there any syntax to see how much memory the code is using?.

Regards

Tridib

On Wed, 24 Nov 2010 13:02:09 +0900, Tridib B.
[email protected] wrote:

GC::Profiler which you can call from Ruby. As people have already
pointed
out any Ruby program will require memory allocation and hence GC.

Can you tell me how to call those two?..And do I need to write some
print statement within those one.??

Regards

There doesn’t seem to be any mystery here, just search for GC_NOTIFY in
gc.c and set it to true rather than false. E.g.

#define GC_NOTIFY 0

becomes

#define GC_NOTIFY 1

That change alone prints “start garbage_collect()” whenever
garbage_collect() is run:

GUTTEA$ ./ruby -e ‘GC.start’
start garbage_collect()
end garbage_collect()

Defining RUBY_MARK_FREE_DEBUG in gc.h gives lots more info as well:

GUTTEA$ ./ruby -e ‘GC.start’
start garbage_collect()
mark: → iseq (0x100624900)
require @ internal:lib/rubygems/custom_require
mark: → iseq (0x100624cb0)
rescue in require @ internal:lib/rubygems/custom_require
mark: ← iseq (0x100624cb0)
mark: ← iseq (0x100624900)
mark: → iseq (0x100612b90)
gem @ internal:gem_prelude
mark: ← iseq (0x100612b90)
mark: → iseq (0x10060c950)

[…]

I don’t see any problem with adding your own debug statements as well if
you want:

static int
garbage_collect(rb_objspace_t *objspace)
{
struct gc_list *list;
rb_thread_t *th = GET_THREAD();
INIT_GC_PROF_PARAMS;

if (GC_NOTIFY) printf("start garbage_collect()\n");

printf("WoopWoop\n");

[…]

GUTTEA$ ./ruby -e ‘GC.start’
start garbage_collect()
WoopWoop

[…]

@Alex G.

I am using Ruby 1.8.1

And I don’t have that GC_NOTIFY function

What to do??..

I have attached my gc.c file…Can you make the correction with
notifying me where you did it?/

Alex G. wrote in post #962544:

On Fri, 19 Nov 2010 10:30:30 +0900, Tridib B.
[email protected] wrote:

which i tried to print out.?

Regards

Tridib

Sure. There’s also the GC_NOTIFY and GC_DEBUG defines in gc.c and
GC::Profiler which you can call from Ruby. As people have already
pointed
out any Ruby program will require memory allocation and hence GC.

Can you tell me how to call those two?..And do I need to write some
print statement within those one.??

Regards

On Tue, 30 Nov 2010 14:02:07 +0900, Tridib B.
[email protected] wrote:

Attachments:
http://www.ruby-forum.com/attachment/5489/gc.c

No, sorry. Your version is very out of date (~2003 I think!?!). You’ll
get
far more help from people if you use a more recent version.

Tridib B. wrote in post #965077:

But I am running on CentOS which supports this Ruby version only…I
tried to compiler Ruby 1.9 but it was not working…

Compile 1.8.6 or 1.8.7, it will be fine. (Assuming you’ve got a working
C compiler of course). Or use rvm which takes care of most of this for
you.

There are also ruby 1.8.5 RPM packages for CentOS 4 in the Testing
repository:
http://dev.centos.org/centos/4/testing/i386/RPMS/

Regards,

Brian.

Alex G. wrote in post #965059:

On Tue, 30 Nov 2010 14:02:07 +0900, Tridib B.
[email protected] wrote:

Attachments:
http://www.ruby-forum.com/attachment/5489/gc.c

No, sorry. Your version is very out of date (~2003 I think!?!). You’ll
get
far more help from people if you use a more recent version.

But I am running on CentOS which supports this Ruby version only…I
tried to compiler Ruby 1.9 but it was not working…

Can change anything within this gc.c file?

Regards
Tridib

Brian C. wrote in post #965078:

Tridib B. wrote in post #965077:

But I am running on CentOS which supports this Ruby version only…I
tried to compiler Ruby 1.9 but it was not working…

Compile 1.8.6 or 1.8.7, it will be fine. (Assuming you’ve got a working
C compiler of course). Or use rvm which takes care of most of this for
you.

There are also ruby 1.8.5 RPM packages for CentOS 4 in the Testing
repository:
http://dev.centos.org/centos/4/testing/i386/RPMS/

Regards,

Brian.

Okay I Have downloaded the Ruby 1.8.6

Can any one say me where is the GC_NOTIFY function in gc.c file?..May be
at which line…

Regards
Tridib

On Tue, Nov 30, 2010 at 4:34 PM, Tridib B.
[email protected] wrote:

Can any one say me where is the GC_NOTIFY function in gc.c file?..May be
at which line…

Use an editor with a search function?


Phillip G.

Though the folk I have met,
(Ah, how soon!) they forget
When I’ve moved on to some other place,
There may be one or two,
When I’ve played and passed through,
Who’ll remember my song or my face.

Phillip G. wrote in post #965134:

On Tue, Nov 30, 2010 at 4:34 PM, Tridib B.
[email protected] wrote:

Can any one say me where is the GC_NOTIFY function in gc.c file?..May be
at which line…

Use an editor with a search function?


Phillip G.

There no GC_NOTIFY function under gc.c file in Riby 1.8.6 also…

Any other options of printing out…

Regards

Tridib B. wrote in post #965573:

There is neither of this functions in Ruby 1.8.6 gc.c file also

The functions are:-

GC_NOTIFY ; GC_DEBUG ; GC::Profiler

Even if I am trying to print some of my own debug print statement in the
function VALUE rb_newobj()

but its not showing my print statement

What to change in so that I can know when the Garbage Collector is
working.

Regards

Tridib

Any One to Help me in this issue>.??..Its Urgent,

Regards

Tridib

There is neither of this functions in Ruby 1.8.6 gc.c file also

The functions are:-

GC_NOTIFY ; GC_DEBUG ; GC::Profiler

Even if I am trying to print some of my own debug print statement in the
function VALUE rb_newobj()

but its not showing my print statement

What to change in so that I can know when the Garbage Collector is
working.

Regards

Tridib