I've got content â\u0080\u0099 in some database columns I'm trying to find and replace with ActiveRecord, but Rails runtime and console appear to have trouble with that string, misinterpreting the first character. I see this in the Rails console, but not in pry or "plain" irb.
When I paste that string into the Rails console, it's converted to \U+FFC3\U+FFA2\, then ignored in the resulting string (see below).
I'd like to understand:
what makes the Rails console different from irb/pry concerning encoding and how I may make it behave similarly regarding encoding
how Rails was able to insert that data in the database in the first place (I'm having issues creating a unit test for this scenario for what I think are the same reasons as the console)
I'm on OS X Mojave using rbenv. I've tried readline and environment variable solutions with no change.
bin/rails console
Running via Spring preloader in process 99194
Loading development environment (Rails 5.2.2)
Cannot read termcap database;
using dumb terminal settings.
irb(main):001:0> RUBY_VERSION
=> "2.5.3"
irb(main):002:0> Encoding::default_internal
=> #<Encoding:UTF-8>
irb(main):004:0> s = '\U+FFC3\U+FFA2\u0080\u0099'
=> "\\u0080\\u0099"
irb(main):005:0> s = "\U+FFC3\U+FFA2\u0080\u0099"
=> "\u0080\u0099"
irb
irb(main):018:0> RUBY_VERSION
=> "2.5.3"
irb(main):019:0> s = 'â\u0080\u0099'
=> "â\\u0080\\u0099"
pry
$ gem list | grep pry
pry (0.12.2)
$ pry
[1] pry(main)> RUBY_VERSION
=> "2.5.3"
[2] pry(main)> s = "â\u0080\u0099"
=> "â\u0080\u0099"
Etc.
I noticed that Encoding::default_internal resolves to #<Encoding:UTF-8> in the Rails console, but to nil in irb and pry. Setting it to nil in the Rails console also did not solve my issue.
Ultimately these strings display like St. Patrickâs Day in the browser
Related
Ruby version: 2.2.5
Rails version: '~> 4.0.13'
Why I and my friend have received different result when trying to convert a string to YAML using YAML.dump method?
[1] pry(main)> YAML.dump("foo")
=> "--- foo\n"
[2] pry(main)> "foo".to_yaml
=> "--- foo\n"
While he has:
[1] pry(main)> YAML.dump("foo")
=> "--- foo\n...\n"
[2] pry(main)> "foo".to_yaml
=> "--- foo\n...\n"
With triple dots after new line (...)
UPDATED
I have confirmed that my rails is using Psych as YAML parser engine, in rails console:
2.2.5 :002 > YAML
=> Psych
2.2.5 :004 > Psych.dump("foo")
=> "--- foo\n"
2.2.5 :005 > YAML.dump("foo")
=> "--- foo\n"
2.2.5 :006 >
But still the result is somehow different. For additional informations, I don't have any syck gem installed and not required it in any files in my rails project.
It appears "You" might be using syck as a YAML processor while "He" is using psych. E.g.
require 'syck'
require 'psych'
Syck.dump("foo")
#=> "--- foo\n"
Psych.dump("foo")
#=> "--- foo\n...\n"
Both are valid YAML parser/emitters per se although Psych has been preferred since 1.9 and Syck really only exists as a gem for backwards compatibility and was completely removed from ruby standard lib as of 2.0.0
As for why "You" are somehow using Syck instead I cannot say without far more information than was provided in this post.
I met this issue this week and spent a great deal of time investigating the root cause. I have found that the difference of libyaml in the system will affect the dump.
For libyaml version 0.1.7, it will always include the ending characters like that, that's why you have the extra "...\n".
For libyaml version 0.2.1 and above, it supports implicit ending so those characters can be ignored
I am using pry for rails console.
Whenever I evaluate some value in console, NoMethodError is raised in bindings_equal?, that is a method of pry-stack_explorer gem. The input and output is as follows.
That is not harmful but a nuisance. I want to remove it.
Can anyone help?
> bundle exec rails console
Loading development environment (Rails 4.1.5)
Frame number: 0/21
[1] pry(main)>
[2] pry(main)>
[3] pry(main)> 1
when_started hook failed: NoMethodError: private method `eval' called for nil:NilClass
{My Bundle Directory}/ruby/2.1.0/gems/pry-stack_explorer-0.4.9.1/lib/pry-stack_explorer.rb:109:in `bindings_equal?'
(see _pry_.hooks.errors to debug)
=> 1
[4] pry(main)>
I use these gems relating to pry:
gem 'pry-rails'
gem 'pry-remote'
gem 'pry-byebug'
Thank you.
I had to add
require 'awesome_print'
AwesomePrint.pry!
to my ~/.pryrc (per the AwesomePrint docs) to fix this error.
Some are mentioning removing Pry.config.print from their ~/.pryrc but I didn't even have a ~/.pryrc, so it appears this can happen by just using awesome_print and pry together.
I'm not running the same gems as you, but have run across the same error.
I started an issue on the github project
Sometimes I have reason to want to start the rails console as an irb repl rather than pry (as awesome as pry is). It will default to pry because pry has in the Gemfile. Hows is that done nowadays?
I think there used to be a --irb option when running rails console but that seems to be gone now. I get a deprecation error message when I try it.
More details
If I just run "rails console" it takes me to pry.
If I run "rails console -irb=irb":
$ rails c -irb=irb
--irb option is no longer supported. Invoke `/your/choice/of/ruby script/rails console` instead
Relevent lines from my Gemfile:
gem 'rails', '3.2.18'
gem 'pry-rails'
gem 'pry-plus'
Launching pry when calling rails console or rails c is set up by the pry-rails gem. If you look in the pry-rails issues there is one that describes a solution.
Define the environment variable DISABLE_PRY_RAILS as 1.
So you can call rails console without pry with:
DISABLE_PRY_RAILS=1 rails c
Works in Rails 4: In your application.rb, inside your Application class, drop this puppy in.
# Use the IRB console instead of the Pry one
console do
require 'irb'
config.console = IRB
end
I couldn't take the Pry console anymore. It kept putting my cursor in odd places at unpredictable times. I can't even describe it but if you know what I'm talking about and know the solution, please let me know.
Inspired by the answers above, I added the following to the class definition in application.rb so that Pry is toggleable from the console:
console do
if ENV['IRB']
require 'irb'
config.console = IRB
end
end
You can then run rails c to get a Pry console, and IRB=true rails c to get an IRB console. This is easily modified if you want the inverse. Works in Rails 4 and 5.
For the benefit of anyone who runs into the same problem, this is my (crappy) workaround.
I wrapped the pry gems in Gemfile with this:
...
unless ENV['NOPRY']
gem 'pry-rails'
gem 'pry-plus'
end
...
Then run this from the unix terminal:
NOPRY=true bundle install
NOPRY=true rails console
Not pretty, but gets the job done...
You can also do it once console has already been started via IRB.start method.
Trying to create an IMGKit instance in one of my Rails controllers:
def wkhtmltoimage
...
kit = IMGKit.new("http://#{request.host_with_port}/?auth_token=" + token)
img = kit.to_img(:jpg)
file = kit.to_file(Rails.root + "public/uploads/testimage.jpg")
...
end
When I load the page, I get this:
NameError in PagesController#wkhtmltoimage
uninitialized constant PagesController::IMGKit
In my Gemfile I'm including the proper gem:
gem 'imgkit'
Bundler installs it fine. When I run the Rails console it is there and works:
1.9.3p194 :005 > IMGKit.new("http://google.com")
=> #<IMGKit:0x007fb08d16a658 #source=http://google.com, #stylesheets=[], #options={}>
1.9.3p194 :006 >
Why would the class IMGKit work in the Rails console but not in the app? Any suggestions? Using RVM, Pow, Rails 3.2.6 (and all other gems work fine with that setup).
EDIT WITH ANSWER
Rebooting the pow server appears to have fixed the problem. Just put in terminal:
touch tmp/restart.txt
That should do it.
Rebooting the pow server appears to have fixed the problem. Just put in terminal:
touch tmp/restart.txt
That should do it.
You have kit set as a local variable in your controller. Have you tried setting it as an instance variable in your controller instead (#kit)?
I have a Rails app that's trying to use the gnuplot gem, but Rails won't bother to load it. It's in my Gemfile and I installed it with bundle install. Bundle knows about it:
vp117025:src tim$ bundle show gnuplot
/Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/gnuplot-2.3.6
But it doesn't appear in the Rails console (and isn't accessible from my app), even after an explicit require, which shouldn't be necessary.
vp117025:src tim$ rails console
Loading development environment (Rails 3.1.0)
ruby-1.9.2-p290 :001 > require 'gnuplot'
=> false
ruby-1.9.2-p290 :002 > require 'gnuplot.rb'
=> false
Now, check to see whether the name of the module is in the constants table:
ruby-1.9.2-p290 :003 > Module.constants.include? :Gnuplot
=> false
It isn't! The module must not really be accessible. This is happening even though the require search path contains the directory that holds gnuplot.rb.
ruby-1.9.2-p290 :004 > $:.include? "/Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/gnuplot-2.3.6/lib"
=> true
If I include the file explicitly by its full path, it works!
ruby-1.9.2-p290 :005 > require "/Users/tim/.rvm/gems/ruby-1.9.2-p290/gems/gnuplot-2.3.6/lib/gnuplot.rb"
=> true
ruby-1.9.2-p290 :006 > Module.constants.include? :Gnuplot
=> true
The module is now visible to the interpreter. It works just fine from outside Rails.
vp117025:src tim$ irb -rubygems -r gnuplot
ruby-1.9.2-p290 :001 > Module.constants.include? :Gnuplot
=> true
Why can't the Rails environment load gnuplot.rb, even though its parent directory is in the search path?
It is loading. The return value of a require statement will be false if the library was already loaded. When you start the Rails console, it requires all the gems in your bundle, so requiring it again will give you false as you see here. Require treats every path passed to it fairly literally, so you can require a file by both its relative and absolute paths, and it will load both times. That's why you get a true result when using the full path – Ruby is treating it as a different file.