Ruby is preinstalled on my Mac and so I wanted to have a look at it. First thing I noticed, is that irb prompts >> instead of irb(main):001:0>. I can't find anything on how to change this with Google because everyone is using irb(main):001:0> in their code ;-)
Can you help me out?
PS: It's not that I think Ruby is broken, but I want to look more nerdy while programming ;-)
$ irb --help
Usage: irb.rb [options] [programfile] [arguments]
--prompt prompt-mode
--prompt-mode prompt-mode
Switch prompt mode. Pre-defined prompt modes are
`default', `simple', `xmp' and `inf-ruby'
$ irb --prompt inf-ruby
irb(main):001:0>
What I do is make that into an alias in my .bashrc so I don't have to type it every time.
echo alias irb=\'irb --prompt inf-ruby\' >> ~/.bashrc
Hope this helps!
goto the location /home/leapfrog/.rvm/scripts
cd ~/.rvm/scripts
Open the file ‘irbrc.rb’, use superuser power to over-write the
$ sudo gedit irbrc.rb
Change the content of the hash '#prompt' to the following
#prompt = {
:PROMPT_I => "#{rvm_ruby_string} :%03n > ", # default prompt
:PROMPT_S => "#{rvm_ruby_string} :%03n%l> ", # known continuation
:PROMPT_C => "#{rvm_ruby_string} :%03n > ",
:PROMPT_N => "#{rvm_ruby_string} :%03n?> ", # unknown continuation
:RETURN => " => %s \n",
:AUTO_INDENT => true
}
Hope this help you :)
Related
I have a space separated string of IPs that I am exporting to a Vagrantfile which I want to iterate over.
export IP="10.10.10.10 10.10.10.11"
I want to perform an operation on this so it becomes a list in the vagrantfile to iterate over.
["10.10.10.10", "10.10.10.11"]
What is the way to do this?
Try figure it out yourself in bash:
$ export IP="10.10.10.10 10.10.10.11"
$ irb # interactive ruby
> puts ENV['IP'] # make sure IP is not nil
10.10.10.10 10.10.10.11 # output
> IPs = ENV['IP'].split
> puts IPs
Vagrantfile is a Ruby script, so you can use ENV['IP'].split in it
The following should be robust. You would not need to worry about padding by space characters at the beginning or the end of the string, or about irregular sequences of space characters.
"10.10.10.10 10.10.10.11".scan(/\S+/)
# => ["10.10.10.10", "10.10.10.11"]
You can just use Split directly as from the examples,
" now's the time".split
=> ["now's", "the", "time"]
>> "10.10.10.10 10.10.10.11".split
=> ["10.10.10.10", "10.10.10.11"]
>> "10.10.10.10 10.10.10.11".split
=> ["10.10.10.10", "10.10.10.11"]
>> " 10.10.10.10 10.10.10.11".split
=> ["10.10.10.10", "10.10.10.11"]
>> "".split
=> []
Read the Docs here
To include variables in vagrant file, Refer this
Use split in a simple way
"10.10.10.10 10.10.10.11".split(' ')
=> ["10.10.10.10", "10.10.10.11"]
I have a password like 'X&Y' and I am trying to run a Ruby script that opens an SSH session, but the script breaks at the & character like :
*server: X
*server: bash: Y: command not found
Escaping the character like & doesn't help either. Ideas appreciated!
The code where it happens is at the ssh.exec:
pass="X\&Y"
Net::SSH.start( host_name, user, :password => pass ) do |ssh|
#do stuff
command = "sudo -S rm file"
cmd = "#{pass}|#{command}"
ssh.exec(cmd) do |ch, stream, data|
puts "*server:" + data.inspect
end
end
You can use ssh like without & getting any special meaning:
ssh -t -t user#localhost "echo 'abc&def'"
abc&def
Connection to localhost closed.
My IRB console output.
1.9.3p385 :005 > "Hello #{#'world'}"
1.9.3p385 :006 >
Shouldn't this return Hello? What is that happening? Is this an error?
You need to remove the second "#" character. That is making the rest of the line a comment. Therefore, it is not a complete line.
"Hello #{'world'}"
Like rjenkins says, " therefore it is not a complete line."
To prove this, and to complete the line in ERB do this:
Type "Hello #{#'world'}", then press Enter. Then type }" and press Enter. You shall see what happens then, once you have "completed the line".
I just updated my ruby version and now when I run irb in command line, I get this weird:
1.9.2p290 :001 >
every line. Before it was a simple >
How can I get it back again?
You can edit your ~/.irbrc file to change the prompt (command line prefix). See this answer for an example. You could put this in there to start:
IRB.conf[:PROMPT][:CUSTOM] = {:PROMPT_I => ">> "}
IRB.conf[:PROMPT_MODE] = :CUSTOM
IRB.conf[:AUTO_INDENT] = true
.irbrc is a Ruby script that irb runs when it starts up that lets you configure your prompt.
From this article, in your user dir ~/, create the .irbrc file with following:
IRB.conf[:PROMPT][:MY_PROMPT] = { # name of prompt mode
:PROMPT_I => ">", # normal prompt
:PROMPT_S => nil, # prompt for continuated strings
:PROMPT_C => nil, # prompt for continuated statement
:RETURN => "=> %s\n" # format to return value
}
IRB.conf[:PROMPT_MODE] = :MY_PROMPT
I try to modify "/foo/bar/dir" to "\/foo\/bar\/dir" by ruby gsub command.
I test it in irb the result is
x = "/foo/bar/dir"
x.gsub("/","\/")
=> "/foo/bar/dir"
x.gsub("/","\\/")
=> "\\/foo\\/bar\\/dir"
Is it possible to replace "/" with "/" by gsub ?
Source of problems:
I try to execute "string in command line" and "real_path" is my variable
real_path = "/home/me/www/idata"
path = real_path.gsub("/","\\/")
=> \\/home\\/me\\/www\\/idata
# But what I expect is \/home\/me\/www\/idata
run "sed 's/SHARE_PATH/#{path}/g' #{path}/config/sphinx.yml > #{path}/config/sphinx.tmp.yml"
result from "run" command is
"sh -c 'sed '\''s/SHARE_PATH/\\/home\\/me\\/www\\/idata\\/shared/g .... "
I need is only one back slash like
"sh -c 'sed '\''s/SHARE_PATH/\/home\/me\/www\/idata\/shared/g .... "
"run" is command from Capistrano
my solution is
use single quote instead of double quote like this
path = real_path.gsub("/",'\/')
You have written:
x = "/foo/bar/dir"
x.gsub("/","\\/")
=> "\\/foo\\/bar\\/dir"
so You did what You had asked before. x.gsub("/","\\/") in fact evaluates to "\/foo\/bar\/dir" but irb prints return value of inspect method instead of to_s.
Edit: Did You mean
real_path.gsub("/","\/")
istead of
real_path.gsub("\/","\/")
Anyway the output is correct - You changed / with \/ so You have
"sh -c 'sed '\''s/SHARE_PATH/\/home\/me\/www\/idata\/shared/g'\'' .... "`
instead of
`"sh -c 'sed '\''s/SHARE_PATH//home/me/www/idata/shared/g'\'' .... "`
and result is different from irb's result (notice the lack of doubled backslash).
For path manipulation I recommend using File.join (documentation)
By the way: why are You modifying the path this way? (1)
Edit2: Why are You asking about changing "/" to "/" but write the following line?
path = real_path.gsub("\/","\\/")
What are You trying to achieve? And what is Your answer to question (1) ?
Edit3:
Here We go:
>> real_path = "/foo/bar/dir"
=> "/foo/bar/dir"
>> path = real_path.gsub("/", "\\/")
=> "\\/foo\\/bar\\/dir"
>> puts "sed 's/SHARE_PATH/#{path}/g' #{path}/config/sphinx.yml > #{path}/config/sphinx.tmp.yml"
sed 's/SHARE_PATH/\/foo\/bar\/dir/g' \/foo\/bar\/dir/config/sphinx.yml > \/foo\/bar\/dir/config/sphinx.tmp.yml
=> nil
>>
but I do not understand why You need backslash in a path?
Yes
irb(main):028:0> (t = x.gsub("/", "\\/")) && nil
=> nil
irb(main):029:0> t
=> "\\/foo\\/bar\\/dir"
irb(main):030:0> puts t
\/foo\/bar\/dir
=> nil
Your first example actually did what you wanted, but the .inspect method that irb is using is escaping backslashes, so it looked like there were extras. If you had used puts you would have seen the real result.