Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does $:.push do in ruby?

I found this in Gemspec file of surveyor gem. What does the following line do?

$:.push File.expand_path("../lib", __FILE__)
require "surveyor/version"

Why does the $:.push thing do? To me it looks like its just requires the ../lib/surveyor/version file. if so, can't I just replace that with following one line?

require File.expand_path('../lib/surveyor/version', __FILE__)

Are both these same thing? If not, then what the difference?

like image 358
CuriousMind Avatar asked Apr 29 '12 14:04

CuriousMind


People also ask

What is the push method in Ruby?

push() is a Ruby array method that is used to add elements at the end of an array. This method returns the array itself. One important aspect of this is that this method can be chained.

What push () will do?

The push() method adds new items to the end of an array. The push() method changes the length of the array. The push() method returns the new length.

What does .POP do in Ruby?

pop() in Ruby? pop() is a Ruby array method that pops or removes the last element of a given array. It permanently removes the last element of an array.


1 Answers

$: is Ruby's load path, so it's in fact adding the a subfolder /lib of a folder in which __FILE__ resides to this array, so that other files from this gem can be required.

like image 102
Mchl Avatar answered Sep 30 '22 01:09

Mchl