ENV['COLUMNS']

P

pedro mg

Hi,

in irb I can have access to ENV['COLUMNS'] for the # of columns in the
shell, and works fine.
But inside a script (ruby script.rb), I can't have access to that hash
key and value.
I've done a:

puts ENV.to_a

and i can not see both COLUMNS and LINES
What would be the problem ?
 
B

Brian Candler

in irb I can have access to ENV['COLUMNS'] for the # of columns in the
shell, and works fine.
But inside a script (ruby script.rb), I can't have access to that hash
key and value.
I've done a:

puts ENV.to_a

and i can not see both COLUMNS and LINES
What would be the problem ?

Nothing to do with ruby...

brian@mappit:~$ echo $COLUMNS
80
brian@mappit:~$ printenv | grep COLUMNS
brian@mappit:~$

This is a shell-internal variable, and not exported by default to processes
launched from the shell. But it can be if you wish:

brian@mappit:~$ export COLUMNS
brian@mappit:~$ printenv | grep 80
COLUMNS=80
brian@mappit:~$

Brian.
 
B

Brian Candler

Well, if that were the case, the variable wouldn't be visible within
irb, either. Since it is, then it must be an exported environment
variable and not just shell internal.

Well, on my system:

brian@mappit:~$ irb1.8
irb(main):001:0> ENV.size
=> 39
irb(main):002:0> exit
brian@mappit:~$ ruby1.8 -e 'puts ENV.size'
37
brian@mappit:~$ printenv | wc -l
37
brian@mappit:~$

So my guess is that irb is setting these variables itself, or calling some
library which does so (readline perhaps?)
I think that the problem has to do with how script.rb is being invoked.
Is it started from something other than a terminal session? ... perhaps
via a cron job or a startup daemon? If so, then LINES and COLUMNS would
not normally be set, since those environment variables are usually
initialized and exported only within an interactive shell.

I thought of that, but all the examples shown above and in my previous reply
were typed into a terminal session.

Regards,

Brian.
 
A

akbarhome

Hi,

in irb I can have access to ENV['COLUMNS'] for the # of columns in the
shell, and works fine.
But inside a script (ruby script.rb), I can't have access to that hash
key and value.
I've done a:

Because when you play with irb, it is clear that you use terminal/
console so ENV['COLUMNS'] does matter. But when you execute ruby
script, it is not clear whether this ruby script need terminal so
ENV['COLUMNS'] does not matter.
 
P

pedro mg

akbarhome said:
in irb I can have access to ENV['COLUMNS'] for the # of columns in the
shell, and works fine.
But inside a script (ruby script.rb), I can't have access to that hash
key and value.
I've done a:

Because when you play with irb, it is clear that you use terminal/
console so ENV['COLUMNS'] does matter. But when you execute ruby
script, it is not clear whether this ruby script need terminal so
ENV['COLUMNS'] does not matter.

sorry but that seems not to be a satisfactory explanation.
Just because it is not clear whether the ruby script needs terminal,
that features is cutted out ?

In this case, i need to know the # of terminal window columns for my
terminal client app. So, it matters.
Is there any way to get it ?
 
P

pedro mg

akbarhome said:
Because when you play with irb, it is clear that you use terminal/
console so ENV['COLUMNS'] does matter. But when you execute ruby
script, it is not clear whether this ruby script need terminal so
ENV['COLUMNS'] does not matter.

akbarhome, unless what you mean is: that info is not available because
you need to call another lib ;) Is that what you meant ?
 
P

pedro mg

Brian said:
I thought of that, but all the examples shown above and in my previous reply
were typed into a terminal session.

yes Brian, me too. I'm running the script in a terminal shell.
 
R

Ryan Davis

Hi,

in irb I can have access to ENV['COLUMNS'] for the # of columns in
the shell, and works fine.
But inside a script (ruby script.rb), I can't have access to that
hash key and value.
I've done a:

puts ENV.to_a

and i can not see both COLUMNS and LINES
What would be the problem ?

COLUMNS isn't exported. You shouldn't be able to see it within your
irb session without exporting it or doing something else special.

% echo $COLUMNS
80
% ruby -e 'p ENV.keys.grep(/COL/)'
[]
% irb[]
=> nil
% export COLUMNS
% ruby -e 'p ENV.keys.grep(/COL/)'
["COLUMNS"]
% irb["COLUMNS"]
=> nil
I suggest you use stty instead.

% ruby -e 'p `stty size`.scan(/\d+/).map { |s| s.to_i }'
[24, 80]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top