How to Pass <Arguments> to Irb If I Don't Specify <Programfile>

Reset IRB Console

Type

exec($0)

in your irb console session.

How to require files by default without mentioning explicitly

Ruby 1.9 help:

$ ruby --help
Usage: ruby [switches] [--] [programfile] [arguments]
-0[octal] specify record separator (\0, if no argument)
-a autosplit mode with -n or -p (splits $_ into $F)
-c check syntax only
-Cdirectory cd to directory, before executing your script
-d set debugging flags (set $DEBUG to true)
-e 'command' one line of script. Several -e's allowed. Omit [programfile]
-Eex[:in] specify the default external and internal character encodings
-Fpattern split() pattern for autosplit (-a)
-i[extension] edit ARGV files in place (make backup if extension supplied)
-Idirectory specify $LOAD_PATH directory (may be used more than once)
-l enable line ending processing
-n assume 'while gets(); ... end' loop around your script
-p assume loop like -n but print line also like sed
-rlibrary require the library, before executing your script
-s enable some switch parsing for switches after script name
-S look for the script using PATH environment variable
-T[level=1] turn on tainting checks
-v print version number, then turn on verbose mode
-w turn warnings on for your script
-W[level=2] set warning level; 0=silence, 1=medium, 2=verbose
-x[directory] strip off text before #!ruby line and perhaps cd to directory
--copyright print the copyright
--version print the version

Take a look on:

-rlibrary       require the library, before executing your script

Why is the --simple-prompt command not working?

You seem to be confused about passing flags to commands vs issuing statements in a REPL.

To start irb with the --simple-prompt option enabled, pass it in like so:

$ irb --simple-prompt
>>

Then you should be able to execute Ruby code.

>> puts "hello world!"
hello world!
=> nil
>>

Couldn't understand how the command line option -T is used in practice

It sets the $SAFE level.

This dictates how inputs are handled, along with a great number of other things regarding environment variables, I/O, threads, exceptions, interpreter command line args, etc.

http://www.ruby-doc.org/docs/ProgrammingRuby/html/taint.html

IMO the docs are a good place to start. If you have a question about a specific behavior, ask.


To address your comment and your edits:

Yes, I can, but the docs can too, and likely better.

Why does -x not work?

Because the docs say it won't:

$SAFE >= 1

   * The command-line options -e, -i, -I, -r, -s, -S, and -x are not allowed.

[~]$ ruby --help
Usage: ruby [switches] [--] [programfile] [arguments]
# elided
-T[level=1] turn on tainting checks

So the default level if -T is specified with no number is 1, which means $SAFE >= 1, which means exactly what the docs say: -x is not allowed.

Why doesn't puts work?

Difficult to say since you don't actually provide the code you're executing, but most likely, again, as the docs say:

$SAFE >= 4

   * Can't write to files or pipes.

which in ruby: Checking if program exists in $PATH from ruby

True cross-platform solution, works properly on Windows:

# Cross-platform way of finding an executable in the $PATH.
#
# which('ruby') #=> /usr/bin/ruby
def which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
end
end
nil
end

This doesn't use host OS sniffing, and respects $PATHEXT which lists valid file extensions for executables on Windows.

Shelling out to which works on many systems but not all.



Related Topics



Leave a reply



Submit