Scripting & System Administration / Environmental Variables
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this video we're going to look at how we can get access to environmental variables from Ruby and I'm going to be using the interactive Ruby shell to show you this. Now, you might be wondering what is environmental variable? Well, it started in the Unix system where you have variables that are set when the user logs in or when the system starts up that you can gain access from various programs that take global values that might pertain to the system or to the user. And the path if one of those and so you can see here is my path, my local variable for path. And this is used in a, in a number of ways, but the primary way is to look for executable programs. So when I type in something like, just Ruby and ask it, hey, what's your version, to get us some output here. Well, obviously it's not sitting wherever I'm at right here. It's not, doesn't exist here anywhere. So the system has to know where to look on your, in your disk for where that program is. So it looks using the path variable. First it looks in that directory, then that directory and so on. So these are all separate directories, separated by colons. On Windows systems, it has the same thing. It's even called path, but it's a semicolon separating these instead of a colon. And that's just one. If I look on, this is a Mac OSX system, Linux is pretty similar. In Windows it's in a different place. You have to go into my computer and you go, I can't remember off the top of my head, but there's a tab in there that you go into and you look at environmental settings and it's in there and it's going to be a similar kind of thing. So if I look at my bash file, I think it's login, there we go, I just have a couple of things in there. I have a path. We already looked at that. And then I have this Ruby opt, which I think we set that up in a prior video when we looked at gems, but this sets up this Ruby, RubyGems and it's an option for Ruby when Ruby runs. It goes ahead and loads up RubyGems right away so I don't have to keep on telling it to load RubyGems because thats an external library. But that's two environmental variables. If you actually do echo Ruby out, you'll see it's set to RubyGems and I could add other ones in there if I want. Now, that's the ones that we want to gain access to. Let me just see if there's any other ones in here. I don't think so. So in order to do that, let's get into IRB. There is a global constant called ENV and I just typed it in, got access to it and you can see it's essentially a hash. So we got our manual path where all our manuals are. That's pretty much a Mac OSX Linux thing. We got the terminal program it's at. What's our terminal? Our shell. Some Perl information. Various environmental information things set up on this system and we already looked at the path. So in order to gain access to this, if you notice, it's a hash so we just simply put in the term that we're looking for, oh, that's a bad choice but the, the value that we're looking for or the key I should say, and the value is going to be returned to us. So right here we have a user and I'm logged in as admin, so if I go in here to ENV and put in the hash here, put in user, it's going to come back with that. If I put in that path in here, it's going to come back with that. Now, if I go ahead and set this into a variable there and now current path is going to hold that value, and I can actually go ahead and let's split this up using the split command, which will take that because right now this is a, just a big string but I can go ahead and split it up and on Windows systems you'd use a similar call in here. And there we go. Now we have an array of all of the paths in my path environmental variable. And if I need to iterate across, I'm looking for something or whatever, I can do that in code pretty easily. So that's how to gain access to your environmental variables in Ruby.
Tutorial Information
| Course: | Programming With Ruby |
| Author: | Al Anderson |
| SKU: | 33788 |
| ISBN: | 1-934743-01-1 |
| Release Date: | 2007-08-21 |
| Duration: | 8.5 hrs / 113 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 