I was just playing around with interactive ruby.
Near the beginning (line 138), I did
. irb(main):138:0> ['rock','paper','scissors'].index('paper')
=> 1
And that above worked
Then I tried a bunch of lines 139-147 experimenting to get more used to the language
Then I wasn't getting results and I tried some even simpler things I expected would work, 148-154 and didn't get any result.
So it looks like at some point one of my commands might've stopped it from displaying results though i'm not sure what.
I'd like to get it to display the results again. I suppose I could try to exit and go back in but i'd rather a way without doing that,
. irb(main):138:0> ['rock','paper','scissors'].index('paper')
=> 1
irb(main):139:0> a=[1,2,3
irb(main):140:1> a
irb(main):141:1> a=[1.2.3]
irb(main):142:1> a[0]
irb(main):143:1> a(0)
irb(main):144:1> a=[1,2,3]
irb(main):145:1> a(1)
irb(main):146:1> puts a(1)
irb(main):147:1> puts a[1]
irb(main):148:1> a
irb(main):149:1> a=[1,2,3]
irb(main):150:1> a
irb(main):151:1> h={4=>4}
irb(main):152:1> h
irb(main):153:1> puts 6
irb(main):154:1>
If it makes any difference this is my version number and OS is Windows.
C:\blah>ruby -v
ruby 2.1.6p336 (2015-04-13 revision 50298) [i386-mingw32]
C:\blah>
Because of this line here:
irb(main):139:0> a=[1,2,3
You haven't closed off the array with a closing ]. the :1 in irb(main):154:1> makes it clear you're inside a nested expression.
If you enter another ], you'll get a big syntax error because all of what you've entered before it isn't valid array syntax, but then you can move on.
Notice that since late 2019, assignment does not output any more the value in irb.
This is a new case where Interactive Ruby (irb) stop showing result.
NB : you can get the old behavior by setting IRB.conf[:ECHO_ON_ASSIGNMENT] = true in your ~/.irbrc.
Related
Here is a sample code called test.rb:
s = %Q_abc\_def\_ghi_
puts s
s = %q_abc\_def\_ghi_
puts s
It works fine as expected:
➜ Desktop ruby test.rb
abc_def_ghi
abc_def_ghi
However, when I run it in irb, nothing happened after s = %q_abc\_def\_ghi_:
➜ Desktop irb
irb(main):001:0> s = %Q_abc\_def\_ghi_
=> "abc_def_ghi"
irb(main):002:0> puts s
abc_def_ghi
=> nil
irb(main):003:0>
irb(main):004:0* s = %q_abc\_def\_ghi_
irb(main):005:1> puts s
irb(main):006:1>
irb(main):007:1*
irb(main):008:1*
Why it won't work? And how can I escape '_' (or other delimiters) in %q notation?
My Ruby version is:
ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
IRB has its own Ruby lexer/parser which it uses to try and keep track of the state of code entered so that it can do things like display different prompts depending on things like if you are in the middle of a string or defining a method or class. The code is the passed to Ruby to be evaluated “properly”.
It looks like this has a bug relating to how it handles escaping of single quoted style strings that aren’t actually using using single quotes.
Ruby itself handles the escaping just fine, so normally I don’t think this bug would actually have much affect, but in your example you happen to have used the string def right after the second _, which is a keyword that IRB also looks for.
This combination appears to put IRB into a strange state where its understanding of what is going on differs from what’s actually happening. This is the odd behaviour you are seeing.
A little playing around with a checked out version of the IRB code seems to support this. The snippet I think is to blame looks like this:
elsif ch == '\\' and #ltype == "'" #'
case ch = getc
when "\\", "\n", "'"
else
ungetc
end
Changing the when line to also look for the actual character being used:
when "\\", "\n", "'", quoted
(quoted is a parameter passed to the function) appears to fix it, and your examples all work fine with this modified version. I don’t know if that is a sufficient fix though, I don’t know the code—this is just a quick hack.
It might be worth opening a bug about this.
I'm not sure why this displays differently in your Ruby file and IRB but lowercase percent strings do not escape. See Difference between '%{}', '%Q{}', '%q{}' in ruby string delimiters
Since %q does not support escaping, there is probably some undefined behavior when you try to use different delimiters and escape characters.
This probably isn't the answer you were looking for but I think it should help a bit.
A simple Ruby program, which works well (using Ruby 2.0.0):
#!/usr/bin/ruby
while gets
print if /foo/../bar/
end
However, Ruby also outputs the warning warning: regex literal in condition. It seems that Ruby considers my flip-flop-expression /foo/../bar/ as dangerous.
My question: Where lies the danger in this program? And: Can I turn off this warning (ideally only for this statement, keeping other warnings active)?
BTW, I found on the net several discussions of this kind of code, also mentioning the warning, but never found a good explanation why we get warned.
You can avoid the warning by using an explicit match:
while gets
print if ~/foo/..~/bar/
end
Regexp#~ matches against $_.
I don't know why the warning is shown (to be honest, I wasn't even aware that Ruby matches regexp literals against $_ implicitly), but according to the parser's source code, it is shown unless you provide the -e option when invoking Ruby, i.e. passing the script as an argument:
$ ruby -e "while gets; print if /foo/../bar/ end"
I would avoid using $_ as an implicit parameter and instead use something like:
while line = gets
print line if line=~/foo/..line=~/bar/
end
I think Neil Slater is right: It looks like a bug in a parser. If I change the code to
#!/usr/bin/ruby
while gets
print if $_=~/foo/..$_=~/bar/
end
the warning disappears.
I'll file a bug report.
Having just started with Ruby and while following a tutorial, the result was shown as:
a + b # => 3
I have never seen such a possibility; that seems so handy! Could you please tell me what it is? is it proprietary or is it for everyone?
Josh Cheek's seeing is believing. Apparently you can run it over your code, or it can be integrated in several editors.
Reconfigure Your REPL
The # symbol is a comment in Ruby. By default, most Ruby REPLs (e.g. irb or pry) will use => to prefix the return value of your last expression.
In IRB, you can modify this prefix so that each return value is prefixed by a different string. You can do this through the IRB::Context#return_format method on your conf instance. For example:
$ irb
irb(main):001:0> conf.return_format = "#=> %s\n"
#=> "#=> %s\n"
irb(main):002:0> 1 + 2
#=> 3
More permanent changes would have to be made in your IRB configuration file by customizing the prompt through the IRB.conf[:PROMPT] Hash and then setting IRB.conf[:PROMPT_MODE] to your custom prompt, but in my opinion the solution above is simpler even though you have to run it within the current REPL session rather than saving it as a default.
Base on all my readings on the web the difference between backtick and system is what is returned . backtick returns STDOUT while system returns true or false.
And I was told that both of them use subshell to perform the operation.
However I am noticing another difference.
output = system('aaa')
puts "output is: #{output}"
output = `aaa`
puts "output is: #{output}"
Result of above code is
$ ruby test.rb
output is:
lab.rb:4:in ``': No such file or directory - aaa (Errno::ENOENT) from test.rb:4:in `<main>'
So it seems in the case of backtick the exceptions are raised to the main program. Operation system swallows the exception and the main program never sees the exception.
I'm using ruby 1.9.3 .
Is my analysis right ?
UPDATE: Got the answer. It's here https://gist.github.com/3730986 .
Copying the answer from the edited question body in order to remove this question from the "Unanswered" filter:
Got the answer. It's here https://gist.github.com/3730986 .
~ answer per nodejs99
I'm a bit confused with the input's of Ruby.
Whenever I try to get input, it doesn't register the 'Backspace' key. Also, it never accepts the 'Enter' first time. I always have to push 'Enter' after my input usually 3 times before it actually inputs it.
For example,
view source
print?
1 my_var = gets.chomp
If I wanted to enter 'Hello', I would have to type it followed by pressing the return key 3 times before it actually entered it.
Now I did find a way to solve this using...
view source
print?
1 STDOUT.flush
2 my_var = gets.chomp
But...
1) This seems wierd having to enter that EVERY time before I want input?
2) It still doesn't solve the problem of registering backspace.
If I was to type directly 'Hello World' but with two accidental keystrokes such as this: Hello Worpold
Even if I used the backspace so it appeared as I was entering: Hello World
If I then went on to 'puts' or 'print' that it would output: Hello Worpold
Know how I can fix it so it accepts backspace and know any other ways of missing out STDOUT.flush?
Thanks in advance
Use the readline module.
What environment are you running Ruby in ? If you're unsure, check with ruby -v
>ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
I'm running v1.8.6 on Windows XP
a = gets
puts "I just got #{a}"
Saved the above snippet to c:\temp.rb and run it with ruby c:\temp.rb
Backspace key works, I can correct strings before pressing enter once to confirm my input.