Error I have gotten a few times and I don't know how to fix it - ruby

I keep seeing undefined (?...) sequence: something. For this particular one I had..
/(?<!\d)[0-3]?\d(?!\d)/
but this has happened a few times to me and I'm not sure how to fix it.
These always work in rubular, but then i get that error when I run it?
Help please!

its working here :
rituraj#rituraj:~$ irb
2.1.1 :001 > s = "somestring 23 and 34 and 233"
=> "somestring 23 and 34 and 233"
2.1.1 :002 > s.scan(/(?<!\d)[0-3]?\d(?!\d)/)
=> ["23", "34"]
check your ruby version:
Ruby's regex engine doesn't support lookbehind which is less than 1.9.
You'd need to switch to 1.9+.
optional usage : you can use oniguruma

Related

unexpected keyword_rescue, expecting keyword_end (SyntaxError) [duplicate]

I have the following ruby code:
EmailTemplate.for(mailer).each do |template|
begin
print '.'
template.upload(publish)
rescue Mandrill::UnknownTemplateError
failed.push(mailer)
end
end
Rubocop corrected my code to:
EmailTemplate.for(mailer).each do |template|
print '.'
template.upload(publish)
rescue Mandrill::UnknownTemplateError
failed.push(mailer)
end
and now it returns following error:
syntax error, unexpected keyword_rescue, expecting keyword_end
How can I fix that?
Rubocop warnings was:
C: Style/RedundantBegin: Redundant begin block detected.
Ruby 2.5.0 added a feature:
rescue/else/ensure are now allowed to be used directly with do/end blocks. [Feature #12906]
But before that, it was not allowed. So syntax error will be there.
Lets do syntax test for the code in sample.rb:
[].each do |a|
# ops
rescue Exception => ex
puts ex.inspect
end
From terminal:
Ruby$ ruby -c sample.rb
sample.rb:3: syntax error, unexpected keyword_rescue
rescue Exception => ex
^
sample.rb:5: syntax error, unexpected keyword_end, expecting end-of-input
Ruby$ rvm use 2.5.1
Using /Users/aruprakshit/.rvm/gems/ruby-2.5.1
Ruby$ ruby -c sample.rb
Syntax OK
See the News. So before 2.5.0, you need to write it like:
[].each do |a|
begin
# ops
rescue => Exception
puts ex.inspect
end
end
You can configure Rubocop to select the version of Ruby you want by following Setting the target Ruby version.
Some checks are dependent on the version of the Ruby interpreter which
the inspected code must run on. For example, enforcing using Ruby 2.3+
safe navigation operator rather than try can help make your code
shorter and more consistent... unless it must run on Ruby 2.2.
If .ruby-version exists in the directory RuboCop is invoked in,
RuboCop will use the version specified by it. Otherwise, users may let
RuboCop know the oldest version of Ruby which your project supports
with:
AllCops:
TargetRubyVersion: 2.4
For some reason, Rubocop thinks you're running Ruby 2.5, not Ruby 2.4.1.
You can fix this one of two ways:
1) Create a file .ruby-version with content 2.4.1. Rubocop should pick up your Ruby version from this file.
2) Add the following to your .rubocop.yml:
AllCops:
TargetRubyVersion: 2.4

What does "cannot load such file -- ruby-debug" indicate when running require?

Hi: I've been asked to debug some ruby code, a language I know next to nothing about, so please bear with me. FWIW, the port is from v 2.1.1 to 2.3.1
This is the ruby script...
#!/usr/bin/env ruby
$VERBOSE = nil
begin
$:.unshift File.expand_path(File.join(File.dirname(File.realpath(__FILE__)), '..', 'lib'))
require 'idl'
rescue LoadError => e
puts "Failed to load idl library. #{e}"
exit(-1)
end
GC.disable # Disable GC since IDL is so transient. The GC takes an inordinate amount of time
IDL::App.instance.run
This is the error message...
Failed to load idl library. cannot load such file -- ruby-debug
The ruby script is in a dir which does have a sibling dir that has the idl library...
-> ll idl ../lib
-rwxrwxr-x 1 joe thegrp 348 Feb 26 17:29 idl
../lib:
total 12
drwxrwsr-x 3 joe thegrp 4096 Feb 21 11:44 idl
-r--r--r-- 1 joe thegrp 4327 Feb 21 11:44 idl.rb
All of this appears to have worked in 2.1.1
I don't think this is a matter where it can't find the library. I stuck some debug statements in the ruby script which just print out the value of... File.expand_path(File.join(File.dirname(File.realpath(FILE)), '..', 'lib')) and it's right on. And remember, this exact same code and dir structure worked in 2.1.1 .
Could it be that the "cannot load such file" is really trying to say "I can't load this library because it was created in 2.1.1 and this is 2.3.1" ??
What's up with the "-- ruby-debug" ?
I found some notes (internal) that "ruby-debug" could be an issue. The subject was...
errors related to deprecated debuggers like pry-debugger, debugger, ruby-debug
The solution was...
Use pry-byebug debugger
I'm not sure why a debugger is being invoked here or if it's the reason for the error. And I'm not sure how to "Use pry-byebug debugger". Is there a way I can stick this in the ruby script, something like "use pry-byebug" ?
Any help deciphering what's happening and any proposed solutions would be appreciated !
I think I just found my own answer.
A .rb file inside the library had a...
use 'ruby-debug'
I changed that to..
use 'pry-bugbye'
and it worked.
IOW, the error message was all about the use of "ruby-debug". And the solution was indeed to replace that with "pry-bugbye"
Hope this posting helps someone else in the future !

Encoding issues with Ruby-2.4.1

I am having different output of following line with different version of ruby:
puts "#{"%c"%[254]}"
Output from ruby-2.4.1 : þ
Output from ruby-1.8.7 : �
It looks like issue with encoding, as there is no encoding in Ruby-1.8.7
Can someone please help me to get same result as Ruby-1.8.7 in Ruby-2.4.1 also?
This result is desirable because Ruby-1.8.7 gives me "254" after decoding while Ruby-2.4.1 gives me following error
`%': invalid value for Integer(): "þ" (ArgumentError)
I don't know much about Ruby 1.8.7, as I started Ruby with version 2.3, but this looks promising: https://gist.github.com/afair/2911107
But as everyone else is saying it would be best to upgrade your Ruby version if at all possible. Older versions of Ruby had limited Unicode support.

ruby, rails gem install error - ERROR: While executing gem ... (Encoding::UndefinedConversionError)

I tried with last version with ruby, but when run gem install rails, always got an error
ERROR: While executing gem ... (Encoding::UndefinedConversionError)
U+041D to IBM437 in conversion from UTF-16LE to UTF-8 to IBM437
I am using windows 8.
but gem list ---local working.. only on install, my locale set english.
what kind problem is it?
Use this link:
https://bugs.ruby-lang.org/issues/10300
They said that you need to chance the enconding at The registry.rb file:
Folder: Ruby2.1.0\lib\ruby\2.1.0\win32
File: registry.rb
Line: 70
- LOCALE = Encoding.find(Encoding.locale_charmap)
+ LOCALE = Encoding::UTF_8
+ #LOCALE = Encoding.find(Encoding.locale_charmap)
We'll need more information to solve your problem. What command are you running?
The meaning of the exception is that some character in the gem is invalid in ibm437, a common 'extended ascii' encoding on Windows machines.
You may be to work around the problem by setting your internal encoding to UTF-8. Encoding.default_internal = Encoding::UTF_8
Would you mind adding the output a running a ruby file containing just the following:
p [Encoding.default_external, Encoding.default_internal, __ENCODING__, Encoding.find('filesystem'), Encoding.find('locale')]
Thanks. If you only do it in irb that's fine to.

sprintf broken in RubyMotion

Ruby noob, just got and installed RubyMotion, created my first program and I'm having trouble getting the Time (sample) app to work.
Whenever I try to format a string using either sprint or the other form (sorry don't know the name) I just get my format string back.
i.e. (from the console while my app is running in Simulator)
Build ./build/iPhoneSimulator-5.1-Development
Simulate ./build/iPhoneSimulator-5.1-Development/Timer.app
(main)>> #time = 0.1
=> 0.0999999940395355
(main)>> string = sprintf("%.1f", #time)
=> "%.1f"
(main)>> string
=> "%.1f"
(main)>> "%.1f" % #time
=> "%.1f"
(main)>>
The same result in the actual app in the Simulator.
I do have the default ruby installed on my Mac but if I try running a test ruby file (print "%05d" % 123) I get expected results.
/usr/bin/ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
/Library/RubyMotion/bin/ruby -v
MacRuby 0.12 (ruby 1.9.2) [universal-darwin11.0, i386]
Running on Lion 10.7.3, any advice or ideas appreciated.
Thanks.
As of 5/9/12, this has been fixed. Make sure you're on RubyMotion 1.3 (run sudo motion update) and string formatting will work again.
Update 5/8/12: Confirmed bug, should be fixed this evening or tomorrow.
Per my comments above, there is definitely a bug with this. An ugly hack to get this working could be something like this (since %d still works fine for sprintf):
#time = 0.1
sprintf("%d", (#time * 10).round).insert(-2, '.')

Resources