[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

rb_require() for strings

zarawesome

12/17/2004 1:00:00 PM

I'd like to use rb_require(), but instead of a filename give it a
string with the contents to be required. How would I go about doing
this?

Thanks in advance
4 Answers

Robert Klemme

12/17/2004 1:10:00 PM

0


"Guilherme T." <zarawesome@gmail.com> schrieb im Newsbeitrag
news:57e813c9.0412170459.47d42421@posting.google.com...
> I'd like to use rb_require(), but instead of a filename give it a
> string with the contents to be required. How would I go about doing
> this?

What you want to do is basically execute a piece of code once, right? In
that case you could do something like this:

Cheap solution:

$code_snippets ||= Hash.new {|h,code| h[code] = eval( code ) }


# will print "buh!" only once
$code_snippets[ 'puts "buh"' ]
# ...
$code_snippets[ 'puts "buh"' ]


A bit nicer

module Code
def self.snippets
@code_snippets ||= Hash.new {|h,code| h[code] = eval( code ) }
end

def self.require( code )
snippets[ code ]
end
end

# will print "bah!" only once
Code.require 'puts "bah!"'
Code.require 'puts "bah!"'
Code.require 'puts "bah!"'
Code.require 'puts "bah!"'

Regards

robert

Charles Mills

12/19/2004 11:34:00 PM

0


Guilherme T. wrote:
> I'd like to use rb_require(), but instead of a filename give it a
> string with the contents to be required. How would I go about doing
> this?
>
> Thanks in advance

I think you want rb_eval_string() from ruby.h
Basically the string you pass is evaluated by the parser. For example:
rb_eval_string("puts 'hey'");
would return Qnil and print "hey\n" to standard out.

Others you may want to consider:
$ grep rb_eval *.h
intern.h:VALUE rb_eval_cmd _((VALUE, VALUE, int));
ruby.h:VALUE rb_eval_string _((const char*));
ruby.h:VALUE rb_eval_string_protect _((const char*, int*));
ruby.h:VALUE rb_eval_string_wrap _((const char*, int*));

-Charlie

TrinhQuocThien

11/4/2010 9:44:00 PM

0

Ma^'y tha(`ng ddo^.c ta`i ddo^.c a'c ma` kho^ng co' thu+.c quye^`n thu
+o+`ng hay che^'t tha?m vi` nha^n da^n no^?i le^n.

Che^'t bo+'t cho ta`n ddo+`i ma^'y tha(`ng ddo^.c ta`i ddo^.c a'c!

A'c gia? a'c ba'o ! ba^'t ta`i a'c ddu+'c thi` chi? ha.i da^n ha.i nu+o
+'c.

nho+' ho.c lo+`i Pha^.t da.y nghen.

A(n hie^`n thi` ga(.p la`nh.

QTTT

> > Và nhu chúng ta dã bi?t thái d? cuong quy?t t? ch?i này dã là nguyên
> > nhân chính d?n d?n cu?c chính bi?n ngày 1 tháng 11 nam 1963 c?a m?t
> > nhóm tu?ng lãnh ph?n lo?n và cái ch?t thê th?m c?a chính T?ng Th?ng
> > Ngô Ðình Di?m và ông C? v?n Ngô Ðình Nhu. Bài h?c l?ch s? th?t d?t
> > giá, dã man và tàn b?o!!
>
> > Lm Tr?n Quý Thi?n
>
> ---------------------------------------------------------------------------­---------------------------------------------------------------
> La`m Linh Mu.c ma` kho^ng chi.u lo ddi gia?ng dda.o ve^` kinh tha'nh,
> lo`ng nha^n tu*`, lo`ng ba'c a'i cu?a Thie^n Chu'a Gia'o cho con
> chie^ng ngoan dda.o nghe du`m....ddi ne` !
>
> Linh Mu.c la.i cu*' hay ba`n loa.n, thi'ch ddua ddo`i la`m chi'nh tri.
> gio^'ng nhu* o^ng Ngo Dinh Thuc, Thich Tri Quang, va` va^n va^n ha?
>
> ;-)
>
> Ca' me` mo^.t lu*'a tho^i !  :-)
>
> (Hay la` Linh Mu.c thi` ddu*o*.c quye^`n la`m chi'nh tri., co`n o^ng
> tha`y chu`a dda^`u tro.c thi` kho^ng ???)

hotac

11/6/2010 3:09:00 PM

0

On Nov 4, 5:43 pm, TrinhQuocThien <trinhquocth...@gmail.com> wrote:
> Ma^'y tha(`ng ddo^.c ta`i ddo^.c a'c ma` kho^ng co' thu+.c quye^`n thu
> +o+`ng hay che^'t tha?m vi` nha^n da^n no^?i le^n.
>
> Che^'t bo+'t cho ta`n ddo+`i ma^'y tha(`ng ddo^.c ta`i ddo^.c a'c!
>
> A'c gia? a'c ba'o ! ba^'t ta`i a'c ddu+'c thi` chi? ha.i da^n ha.i nu+o
> +'c.
>
> nho+' ho.c lo+`i Pha^.t da.y nghen.
>
> A(n hie^`n thi` ga(.p la`nh.
>
> QTTT
>
>

A'c gia? a'c ba'o
A(n hie^`n ga(.p la`nh.

Ma^'y o^ng HT, Thu+o+.ng to.a Thi'ch cu?a A^'n Quang ddem ma^'y chu.c
su+ sa~i ra thie^u dde^? Pha^.t gia'o A^'n Quang gio+` tha`nh tu+o+ng,
tha`nh chao tro^.n chung vo+'i Ba'c Ho^`.