What does $:.push do in ruby?
Asked Answered
F

1

16

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?

Fayfayal answered 29/4, 2012 at 14:27 Comment(3)
Just a side note: $LOAD_PATH << File.expand_path ... does the same and looks a bit less obfuscated, IMHO.Gnathion
indeed, thanks for the tip :)Fayfayal
Symbolhound.com is useful for searching syntax like this.Acropetal
D
17

$: 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.

Denaedenarius answered 29/4, 2012 at 14:35 Comment(2)
Basically a shortcut of having to write the complete path. That explains it. Thanks!Fayfayal
@Gaurish: Not only that. The gem can assume that it is in the load path, in which it will fail to load if you require it by full path, without adding it to the load path.Gnathion

© 2022 - 2024 — McMap. All rights reserved.