As we know, commands below can add new path in the tail of original
ENV[‘path’](i.e. /path_old:/path_new)
ENV[‘PATH’] += “:/path_new”
If I want the new path added in the head of original ENV[‘path’](i.e.
/path_new:/path_old), is there quick way simlilar as “+=”?
Certainly commands below works, just want to know whether simplified
command similiar with “+=”.
ENV[‘PATH’] = ‘/path_new’ + ‘:’ + ‘/path_old’
previn
2
Subject: How to add path in the head of original ENV[‘path’]
Date: gio 16 gen 14 07:13:00 +0100
Quoting Previn L. ([email protected]):
If I want the new path added in the head of original ENV[‘path’](i.e.
/path_new:/path_old), is there quick way simlilar as “+=”?
ENV[‘PATH’].prepend(’/path_new:’)
ENV[‘PATH’] is a string, which can be manipulated like any other
string.
Carlo
previn
3
It would be
ENV[‘PATH’] = ‘/newpath’ + ENV[‘PATH’]
and it is as easy as it can get. Not aware about any method like +=.
previn
4
Arun kant sharma wrote in post #1133323:
It would be
ENV[‘PATH’] = ‘/newpath’ + ENV[‘PATH’]
and it is as easy as it can get. Not aware about any method like +=.
I know, thank you.
previn
5
Carlo E. Prelz wrote in post #1133313:
Subject: How to add path in the head of original ENV[‘path’]
Date: gio 16 gen 14 07:13:00 +0100
Quoting Previn L. ([email protected]):
If I want the new path added in the head of original ENV[‘path’](i.e.
/path_new:/path_old), is there quick way simlilar as “+=”?
ENV[‘PATH’].prepend(’/path_new:’)
ENV[‘PATH’] is a string, which can be manipulated like any other
string.
Carlo
This is just I want, thank you.
previn
6
Carlo E. Prelz wrote in post #1133313:
Subject: How to add path in the head of original ENV[‘path’]
Date: gio 16 gen 14 07:13:00 +0100
Quoting Previn L. ([email protected]):
If I want the new path added in the head of original ENV[‘path’](i.e.
/path_new:/path_old), is there quick way simlilar as “+=”?
ENV[‘PATH’].prepend(’/path_new:’)
ENV[‘PATH’] is a string, which can be manipulated like any other
string.
Carlo
======
- ruby 1.9.2-p320
======
Sorry, it reports error below when use “prepend”, the ruby version used
is 1.9.2-p320 .
undefined method `prepend’ for “/path_old”:String (NoMethodError)
======
2. ruby 2.0.0-p247
in `prepend’: can’t modify frozen String (RuntimeError)
previn
7
Matthew K. wrote in post #1133419:
Arun kant sharma wrote in post #1133323:
It would be
ENV[‘PATH’] = ‘/newpath’ + ENV[‘PATH’]
and it is as easy as it can get. Not aware about any method like +=.
It isn’t a method; a += b' is syntactic sugar for
a = a + b’
And you forgot the path separator:
ENV[‘PATH’] = ‘/newpath:’ + ENV[‘PATH’]
Yes, but I just want to know whether there is more elegant way without
writing ENV[‘PATH’] twice.
previn
8
Previn L. wrote in post #1133420:
Yes, but I just want to know whether there is more elegant way without
writing ENV[‘PATH’] twice.
Well, apparently String#prepend has been a part of ruby since at least
1.9.3
$ ruby -ve ‘p “a”.prepend(“b”)’
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
“ba”
$
If it’s not defined, you could always do this:
class String; def prepend(o) “#{o}#{self}”; end end
It’s not identical in behaviour, but it’s close enough.
previn
9
Arun kant sharma wrote in post #1133323:
It would be
ENV[‘PATH’] = ‘/newpath’ + ENV[‘PATH’]
and it is as easy as it can get. Not aware about any method like +=.
It isn’t a method; a += b' is syntactic sugar for
a = a + b’
And you forgot the path separator:
ENV[‘PATH’] = ‘/newpath:’ + ENV[‘PATH’]
previn
10
Subject: Re: How to add path in the head of original ENV
Date: ven 17 gen 14 05:29:34 +0100
Quoting Previn L. ([email protected]):
Yes, but I just want to know whether there is more elegant way without
writing ENV[‘PATH’] twice.
prepend exists here on 2.2.0dev. But then, if I run
ENV[‘PATH’].prepend(’/path_new:’)
it balks with:
RuntimeError: can’t modify frozen String
:-/
Previn: if I were you I would write ENV[‘PATH’] twice and be over with
it. There are times when one has to be pragmatic.
Carlo
previn
11
Carlo E. Prelz wrote in post #1133425:
Previn: if I were you I would write ENV[‘PATH’] twice and be over with
it. There are times when one has to be pragmatic.
Carlo
Yes, this is not so important, let’s close this case.