On my OS X development system:
$ ruby --version
ruby 1.8.6 (2007-03-13 patchlevel 0) [universal-darwin8.0]
$ script/console
Loading development environment (Rails 2.3.4)
>> require 'yaml'
=> []
On CentOS 5.3 production system:
$ script/console production
Loading production environment (Rails 2.3.4)
/opt/ruby-enterprise-1.8.7-2009.10/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/base.rb:1959:in `method_missing':NoMethodError: undefined method `define_index' for #<Class:0x2e6f7b0>
>> require 'yaml'
=> false
Anything I can do about that NoMethodError?
Why would yaml be unavailable. Isn't it part of the core Ruby libraries?
Returning false means that the loading was successful or was already done. If it couldn't be loaded, it'd raise an exception instead.
It is part of the Ruby core libraries, but I'm not sure what gets bundled up with Ruby Enterprise Edition. Check that yaml.rb is inside your $LOAD_PATH. For example, try this in irb:
$LOAD_PATH.collect { |path| File.join(path, 'yaml.rb') }.find { |path| File.exist?(path) }
On OS X it will produce something like:
=> "/opt/local/lib/ruby/1.8/yaml.rb"
Related
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
I have rails 2.3.4 and rails 2.3.14 gems in my gem directory. Create a project
rails _2.3.4_ foo
Replace the line:
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
with
if true
RAILS_GEM_VERSION = '2.3.4'
else
RAILS_GEM_VERSION = "2.3.14"
end
Now when I run the console, I get:
Loading development environment (Rails 2.3.14)
but if type in:
>> Rails.version
=> "2.3.4"
However, now I just add a comment:
if true
RAILS_GEM_VERSION = '2.3.4'
# comment
else
RAILS_GEM_VERSION = '2.3.14'
end
and I launch the console, and get:
Loading development environment (Rails 2.3.4)
Rails version = 2.3.4
Note that now it points to 2.3.4 in the "loading" line. Seems really bizarre to me that a comment will affect this.
The rails bootstrap code that determines which version of rails to use doesn't actually execute your environment.rb - it reads in that file and tries to extract what you set RAILS_GEM_VERSION to. That code is easily confused by conditionals and so on.
If you want precise control over what versions of gems are used I'd recommend using bundler.
I installed the mongrel gem because I need it on my workstation for rare occasions, and now it's my default Rails (2) server. I know I could specify script/server webrick on the command line, but the fact is that I'd like to have my system (or app) default to webrick, and only use mongrel when specified.
Anybody know how to arrange that?
Specs: WinXP, Rails 2.3.12, Ruby 1.8.7
Ok here are a few options:
Option one - One off: Always add webrick as a command line arg
Open script/server and insert a line between the two requires
#!/usr/bin/env ruby
require File.expand_path('../../config/boot', __FILE__)
ARGV.unshift "webrick"
require 'commands/server'
Option two - Global: Edit the commands/server.rb file that launches rails
gem which railties -> tells you where the startup code is
Open the file at lib/commands/server.rb
Around line 45 edit the logic so that webrick is always launched by default.
server = Rack::Handler.get(ARGV.first) rescue nil
unless server
begin
server = Rack::Handler::WEBrick # was Mongrel
rescue LoadError => e
server = Rack::Handler::WEBrick
end
end
Option 3 - Cleanest but most involved:
Switch to Bundler and manage your dependencies directly. This is more work but positions you for switching to rails 3 at some point which might be nice depending on the life cycle of the application.
There's a tutorial for rails 2.3 here
I have an application using Rails 2.3.4 which I'm trying to run. No matter how I start it, by using script/console or script/ferret_server, it always boils down to the same error:
uninitialized constant Gem
When I search online, I only find people who has a similar problem finding something inside of the Gem class. But in my case, Gem is just not there at all.
I am running Ruby on Ubuntu. I tried using the version of Ruby that came with APT and a more recent one with RVM. Nothing helped.
Any idea how what can be causing thing and how to fix it? Thanks in advance!
Some extra details, as requested by commenters:
Output of ruby -v:
ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux], MBARI 0x6770, Ruby Enterprise Edition 2011.03
Output of gem -v:
1.8.6
Calling ruby script/console outputs a similar error (edited):
Loading development environment (Rails 2.3.4)
/var/www/sites/example.com/releases/20110726061310/config/environment.rb:12:NameError: uninitialized constant Gem
/usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb/init.rb:254:in `require': no such file to load -- console_app (LoadError)
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb/init.rb:254:in `load_modules'
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb/init.rb:252:in `each'
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb/init.rb:252:in `load_modules'
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb/init.rb:21:in `setup'
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb.rb:54:in `start'
from /usr/local/rvm/rubies/ree-1.8.7-2011.03/bin/irb:17
I'm trying to call Ferret Server to allow Passenger to connect to it, with the command:
ruby --debug script/ferret_server -e production start
Outputs the following error:
Exception `NameError' at ./script/../vendor/plugins/acts_as_ferret/lib/../../../../config/environment.rb:12 - uninitialized constant Gem
uninitialized constant Gem
Alternatively, calling bundle exec ruby --debug script/ferret_server -e production start outputs the following error:
superclass mismatch for class GemDependency
Please note that I added the following right above the Rails::Initializer.run do |config| line in config/environment.rb:
require 'thread'
if Gem::VERSION >= "1.3.6"
module Rails
class GemDependency
def requirement
r = super
(r == Gem::Requirement.default) ? nil : r
end
end
end
end
require File.join(File.dirname(__FILE__), 'boot')
Remove the lines mentioned above gives uninitialized constant Rails errors instead.
Try putting:
require 'rubygems'
Just after require 'thread'
I'm trying to create a custom daemon that loads up the Rails environment.
My environment is as follows:
ruby-1.9.2-p180
rails 3.0.5
I did the following:
-Installed the daemons gem
-Installed daemon_generator plugin found here:
https://github.com/dougal/daemon_generator
-Generated a daemon: rails generate daemon listener
All this worked fine. When I run the daemon, it works.
However, as soon as I try to access an active record object like trying to retrieve a user, it blows up.
*** below you find the most recent exception thrown, this will be likely (but not certainly) the exception that made the application exit abnormally ***
#<NameError: method `recognize' not defined in Rack::Mount::RouteSet>
*** below you find all exception objects found in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***
#<NoMemoryError: failed to allocate memory>
#<SystemStackError: stack level too deep>
#<fatal: exception reentered>
#<NoMethodError: undefined method `eq' for nil:NilClass>
#<NameError: method `recognize' not defined in Rack::Mount::RouteSet>
Any thoughts on how to create a Daemon that loads up Rails 3.0.5?
I prefer to roll my own rails daemon controllers. Here is a simple example that works for most cases:
script/daemon
#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'
ENV["APP_ROOT"] ||= File.expand_path("#{File.dirname(__FILE__)}/..")
ENV["RAILS_ENV_PATH"] ||= "#{ENV["APP_ROOT"]}/config/environment.rb"
script = "#{ENV["APP_ROOT"]}/daemons/#{ARGV[1]}"
Daemons.run(script, dir_mode: :normal, dir: "#{ENV["APP_ROOT"]}/tmp/pids")
daemons/your_daemon_script.rb
require ENV["RAILS_ENV_PATH"]
loop {
... your code ...
}
You can control your deamons by using the following commands:
script/daemon run your_daemon_script.rb
script/daemon start your_daemon_script.rb
script/daemon stop your_daemon_script.rb
This enables me to easily add new daemons and I can easily load rails in each script if necessary.
I had a lot of problems trying get daemon_generator working. I got my daemon working by skipping the daemon_generator all together and just using the daemons gem (v1.1.3).
in urserver_control.rb (in root ruby app directory):
#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'
require 'TweetMsg'
Daemons.run('urserver.rb')
in urserver.rb:
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), 'config', 'environmen
t'))
require "rubygems"
--- insert your code here ---
You can test by running your server directly ruby urserver.rb or ruby urserver_controller run
And then once that is working starting and stopping the controllerruby urserver_control.rb {start | stop | run }
Looking at the code in https://github.com/dougal/daemon_generator/blob/master/lib/generators/daemon/templates/script.rb it appears they load things in the wrong order...
Looking at my delayed_job daemon script and config.ru they load config/environment.rb (which in turn loads application.rb and initializes the app)
So a possible fix would be to edit the generated script and make it only require 'config/environment.rb'
I tried this:
#!/usr/bin/env ruby
# You might want to change this
ENV["RAILS_ENV"] ||= "development"
require File.dirname(__FILE__) + "/../config/environment"
$running = true
Signal.trap("TERM") do
$running = false
end
while($running) do
# Replace this with your code
Rails.logger.auto_flushing = true
o = Order.last
Rails.logger.info "The latest order is #{o.id}"
sleep 10
end
and it yielded no errors... (Tried both Rails 3.0.3 and 3.0.5)
I had problems running daemon as is on my staging server (Rails 3.0.7, ruby 1.8.7, passenger 3.0.0). Neither
require File.dirname(FILE) + "/../../config/application"
Rails.application.require_environment!
nor
require File.dirname(FILE) + "/../config/environment"
Worked.
I fixed it by re-installing the standard config.ru in rails root (which I had de-installed to integrate w passenger... not sure now how I will get daemons & passenger to work together now ...)