`

$:.unshift File.expand_path('..', __FILE__)

 
阅读更多
一直能看到一些gem里面会有这样一句代码:
$:.unshift File.expand_path('..', __FILE__)
这句话是干什么用的呢
$:就是ruby的一个全局变量,也叫$LOAD_PATH,功能就是java中的classpath,用来加载类库的,当你require某个文件时,ruby就会从这个变量的值去查找,找不到会报LoadError。这个值其实就是一个包含了类库绝对路径的数组。

__FILE__这个变量代表文件自己的文件名,在foo.rb中puts__FILE__,结果就是foo.rb。

File.expand_path可以把路径转换成绝对路径,假设有这样一个文件/Users/kenshin/foo.rb,里面有File.expand_path('..', __FILE__),返回的结果就是/Users/kenshin。

unshift是数组的一个方法,功能就是把指定的值加到数组的最前面,[3,4].unshift(1,2) => [1,2,3,4]

所以这段代码的意思就是把当前文件所在的目录加到ruby的loadpath的最前面,在require文件时,ruby就会先从当前目录下去查找了。
分享到:
评论

相关推荐

    ib:用于rubymotion的IB插座

    unshift ( "/Library/RubyMotion/lib" ) require 'motion/project' # if you use bundler require 'bundler' Bundler . require # if you are not using bundler require 'rubygems' require 'ib' Motion :: ...

    test:第一个仓库

    lib = File.expand_path("lib", __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require "http/version" Gem::Specification.new do |gem| gem.authors = ["Tony Arcieri", "Erik Michaels-...

    Routable:RubyMotion UIViewController-> URL路由器

    可路由 UIViewController-> URL路由器。 @router . map ( "profile/:id" , ProfileController ) ... unshift ( "/Library/RubyMotion/lib" ) require 'motion/project' require 'routable' Motion

    CodeIgniter实现更改view文件夹路径的方法

    public function add_package_...array_unshift($this->_ci_library_paths, $path); array_unshift($this->_ci_model_paths, $path); array_unshift($this->_ci_helper_paths, $path); $this->_ci_view_paths = arr

    random-lodash-mixins:缺少 Lodash 的某些功能,但在使用例如 AngularJS + Socket.io 时可能很有用

    随机函数 ...var unshift = arrayRef.unshift; // Single element a.unshift(-1); // a = [-1, 1, 2]; // Callback socket.on('users:new', _.bind(_.call, $scope.users.unshift, $scope.users)); //

    flash action script 经典字典教程大全,学flash必备

    flash action script 经典字典教程大全,学flash必备 -- --(递减) ++ ++(递增) ! !(逻辑 NOT) ......._highquality _highquality、Button._highquality、MovieClip._highquality、TextField._highquality ...

    Algorithm:JavaScript算法:grinning_squinting_face::spaghetti:

    :cooking:JavaScript算法的成分 :tomato: ... unshift ( number ) ; number = Math . floor ( number / 10 ) ;} while ( number > 0 ) // arr=[1,5,5] :tomato: 十六进制转换十进制->十六进制let dec = 123 ;let hex

    tweenjs.min.js文件

    e:c,d.paused=a,d.time=b,d.runTime=b-Ticker._pausedTime,Ticker.dispatchEvent(d)}for(Ticker._tickTimes.unshift(Ticker._getTime()-b);Ticker._tickTimes.length>100;)Ticker._tickTimes.pop();for(Ticker._...

    electronicSignature.rar

    小程序手写板电子签名已经全部整理好的代码,把文件粘贴过去就可以直接使用,不需要再修改

    node-manage-path:这个微型库允许您跨平台更改$ PATH

    unshift ( path . join ( process . cwd ( ) , 'node_modules' , '.bin' ) // add one path to the beginning // process.env.PATH now starts with the `.bin` in your `node_modules` directory :-) // unless ...

    $.format,jquery.format 使用说明

    代码如下:$.format = function (source, params) { if (arguments.length == 1) return function () { var args = $.makeArray(arguments); args.unshift(source); return $.format.apply(this, args); }; if ...

    【JavaScript源代码】Vue中v-for更新检测的操作方法.docx

    Vue中v-for更新检测的操作方法  口诀:  数组变更方法,就会导致 v-for 更新,页面更新 数组非变更方法:返回新数组,就不会导致 v-... arr.unshift()从前面添加元素,返回值时添加完后数组长度 arr.unshift(8) 

    PHP获取IP地址及根据IP判断城市

    array_unshift($ips,$ip); $ip=FALSE; } for($i=0;$i($ips);$i++){ if (!eregi("^(10│172.16│192.168).",$ips[$i])){ $ip=$ips[$i]; break; } } } $ip=$ip?$ip:$_SERVER['REMOTE_ADDR']; ...

    Google C++ International Standard.pdf

    Contents Contents ii List of Tables x List of Figures xiv 1 Scope 1 2 Normative references 2 3 Terms and definitions 3 4 General principles 7 4.1 Implementation compliance . ....4.2 Structure of this ...

    CnKore for CRO

    unshift @{$slave->{slave_ai_seq_args}}, ((defined $args) ? $args : {}); } sub clear { my $slave = shift; if (@_) { my $changed; for (my $i = 0; $i $slave->{slave_ai_seq}}; $i++) { if ...

    C++ 标准 ISO 14882-2011

    Contents Contents iii List of Tables xi List of Figures xv 1 General 1 1.1 Scope . . . . ....1.2 Normative references ....1.3 Terms and definitions ....1.4 Implementation compliance ....1.5 Structure of this ...

    小程序开发之登录 附代码

    logs.unshift(Date.now()) wx.setStorageSync('logs', logs) wx.login({ success: function(res) { if (res.code) { //发起网络请求 wx.getUserInfo({ success: function(ures) { var userInfo = ures.userInfo v

    snake-game:Scrimba教程

    蛇游戏教程游戏规则玩蛇的逻辑-蛇吃苹果并长大。 -不能碰到自己或板的...长度Math.floor() Math.random() innerText setTimeout() .textContent .unshift() 。流行音乐() setInterval() .createElment()

    Alien-Autotools:构建并安装GNU构建系统

    unshift @PATH , Alien::Autotools -> bin_dir;unshift @ACLOCAL_PATH , Alien::Autotools -> aclocal_dir;system ' autoconf ' , ...; 从 : use alienfile;share { requires ' Alien::Autotools ' ;};描述该提供了...

    编码挑战

    leetcode,hackerrank和过去的采访中的编码挑战。 每个都有自己的测试用例。...时间复杂度: .push() O(1).pop() O(1).shift() O(n).unshift() O(n).concat() O(n).slice() O(n).splice() O(n).sort()

Global site tag (gtag.js) - Google Analytics