easy require di ruby

di ruby (native) setahu saya kalo mau require biasanya pake absolute path (akan merequire file keren1.rb dan keren2.rb) :

require “/home/didit/dalem/dalem2/../../src/keren1”

require “/home/didit/dalem/dalem2/../../src/keren2”

padahal sekarang kita bisa aja berada di ./home/didit/dalem/dalem2 , wah gak keren ya

ada solusinya kita bisa merubah variabel magic pada ruby untuk urusan search file nya yaitu variabel $:

caranya seperti :

 

$:.unshift File.join(File.expand_path(File.dirname(__FILE__)), “..”, “..”,”src”)

sehingga setalah melakukan itu kita bisa

require “keren1”

require “keren2”

 

happy require..

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s