I am attempting to use the crypt gem. While I am able to get it installed via bundler, when I do:
require 'crypt/rijndael'
I got an error:
no such file to load -- crypt/rijndael
Any ideas?
Which version of Crypt? Which version of Ruby? Where are you trying to require it from? I tried Crypt 1.1.4 on Ruby 1.8.7 and it worked for me in the rails console:
ruby-1.8.7-p334 :001 > require 'crypt/rijndael'
=> ["Crypt"]
ruby-1.8.7-p334 :002 >
If you're using 1.9, try crypt 1.2.1 instead:
https://github.com/titanous/crypt
Haven't tested it myself, but give it a shot if you're on 1.9
Related
I can see from here that has_secure_token can generate a random token for a new record in a (User) model. How can I simply generate a random token without needing to do it for a model? (i.e. suppose an app has no model(s) or to put it really simply, to run some (preferably very simple) method, and it should return a random string like pX27zsMN2ViQKta1bGfLmVJE
I have tried gems SecureRandom and digest as recommended in other answers, but neither seems to bundle install successfully (the SO answers were oldish so perhaps those gems are deprecated?) - I'm using ruby 2.5.1 and rails 5.2.3
My goal is simply to generate tokens to use as keys for an API, so I essentially just need randomly generated strings that are safe to use in urls, perhaps a method that I can specify the length of the token (but I'll take what I can get for now!)
Just use SecureRandom. It's not a gem that you have to install. It's built into Ruby:
⇒ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin18]
⇒ irb
2.5.1 :001 > require 'securerandom'
=> true
2.5.1 :002 > SecureRandom.hex(32)
=> "89d45edb28859a905672b707c8f7599f766d12074584ef48a997230dfc0e0998"
2.5.1 :003 > SecureRandom.base64(12)
=> "KaZbhQ7o7U/f9pMs"
2.5.1 :004 > SecureRandom.uuid
=> "ade17ef5-0943-4c70-b417-df7c96c198cd"
I am using rails 4.1.4, ruby 2.1.2 and rvm.
Gemfile (extract)
gem 'rails-perftest'
gem 'ruby-prof', group: :test
I installed ruby using these commands (in order to apply patch which enables memory profiling)
rvm get stable
rvm reinstall 2.1.2 --patch railsexpress
But still no luck and memory reports are empty with rake test:benchmark or rake test:profile
I was trying to get the same Ruby patch and version working with a Rails 3 benchmark and that was broken too, albeit in a different way. It looks to me like this is an oversight in Rails. I'm seeing this warning string on a Rails 3.2 app
$ bundle exec rake test:benchmark
Update your ruby interpreter to be able to run benchmarks.
$ bundle exec rails -v
Rails 3.2.21
The problem seems to be that ActiveSupport 3.2 isn't aware of Ruby versions higher that 2.0 for this particular piece of code
if RUBY_VERSION.between?('1.9.2', '2.0')
require 'active_support/testing/performance/ruby/yarv'
elsif RUBY_VERSION.between?('1.8.6', '1.9')
require 'active_support/testing/performance/ruby/mri'
else
$stderr.puts 'Update your ruby interpreter to be able to run benchmarks.'
exit
end
see https://www.omniref.com/ruby/gems/activesupport/3.2.12/symbols/ActiveSupport::Testing::Performance::Metrics::CpuTime#line=145
After editing the version check manually I can confirm that the patch does work in Rails 3 with version 2.1.2. Perhaps you could check your RUBY_VERSION and RUBY_ENGINE constants for anything unusual?
(I understand this isn't really an answer but I don't have enough reputation to comment. Also it hopefully rules out the rvm patch and ruby-prof as a problem)
I have a problem when starting rails console:
Loading development environment (Rails 4.1.1)
cannot load such file -- rubygems/custom_require.rb
Console is running, except that I cannot require any gem. Require in application is okay.
I would like to test in console with any of gems, do you now what is the problem?
Ruby version
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]
Thanks.
What version of ruby are you using?
Trying requiring a gem with relative require on the file.
require_relative 'gem_name'
I have a mystery to solve when upgrading our Rails3.2 Ruby 1.9 app to a Rails3.2 Ruby 2.1.2 one. Nokogiri seems to break, in that it changes its behavior using open-uri. No gem versions are changed, just the ruby version (this is all on OSX Mavericks, using brew, gcc4 etc).
Steps to reproduce:
$ ruby -v
ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-darwin13.1.0]
$ rails console
Connecting to database specified by database.yml
Loading development environment (Rails 3.2.18)
> feed = Nokogiri::XML(open(URI.encode("http://anyblog.wordpress.org/feed/")))
=> #(Document:0x3fcb82f08448 {
name = "document",
children = [
..
> feed.xpath("//item").count
=> 10
So all good! Next, after a rvm change to Ruby 2.1.2 and a bundle install..
$ ruby -v
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
$ rails console
Connecting to database specified by database.yml
Loading development environment (Rails 3.2.18)
> feed = Nokogiri::XML(open(URI.encode("http://anyblog.wordpress.org/feed/")))
=>
> feed.inspect
=> "#<Nokogiri::XML::Document:0x86a1f21c name=\"document\">"
> feed.xpath("//item").count
=> 0
So it looks like the 'open' encoding has changed, in that a gzip http stream isn't being fed correctly to nokogiri? I checked with a nokogiri -v and it is using the packaged xml libs rather than system ones. Is this a open-uri Ruby 2.1.2 issue?
Another theory is that one of the gems has monkey patched open-uri to fix something in 1.9 and that is breaking 2.1? Help or ideas please!
EDIT: Here's more info not using Nokogiri, i.e. thinking this is more a open-uri issue on Ruby 2.1.2:
> open(url) {|f|
* f.each_line {|line| p line}
* p f.content_type
* p f.charset
* p f.content_encoding
* }
"\u001F\x8B\b\u0000\u0000\u0000\u0000\u0000\u0000\u0003\xED\x9D\xDBr\eW\xB2\xA6\xAF\xED\xA7\xA8\xCD\u001E\xB7/$\u0010..
(snip)
3\xF3\xA79\xA7\xFAɗ\xFF\u000F\xEAo\x9C\u0014k\xE8\u0000\u0000"
"text/xml"
"utf-8"
["gzip"]
=> ["gzip"]
..the 1.9 version was readable, i.e. gzip was applied already.
If I go into a clean ruby irb it works ok, so it must be something in my rails gems that is changing the behavior of open-uri open to not deflate/gzip. I have a lot of gems referenced.. :(
Ok, here's an answer, and maybe the answer. Ruby 2 changed how it uses headers in HTTP requests and zipping/deflating, but at some point they changed their minds back and put it to be how 1.9 worked. In the interim some Rails gem maintainers monkey patched HTTP:Net to make their gems work on both 1.9 and 2.0. Those monkey patches still linger in older versions of gems and cause issues like I saw upgrading from 1.9 to 2.1
A summary of the issue and solution here:
http://avi.io/blog/2013/12/17/do-not-upgrade-your-rails-project-to-ruby-2-before-you-read-this/
We use the gem right_aws, and the details of that issue with ruby versions is here:
https://github.com/sferik/twitter/issues/473
The solution was to undo the monkey patch using this as a gem reference in our Gemfile:
gem 'right_http_connection', git: 'git://github.com/rightscale/right_http_connection.git', ref: '3359524d81'
Background reading and more info:
https://github.com/rightscale/right_aws/issues/167
Working in Nitrous.IO, my rails app was acting strange. So I checked and I was using rails version 0.9.5 instead of version 4.0.1! I decided to do a gem uninstall rails --version 0.9.5. Everything is good, except it also removed the rails executable! How do I get that back? Seems like version 4.0.1 is there, but I can't run the rails command. Should I just uninstall and reinstall rails?
UPDATE: Tried gem install rails even though I already had rails 4.0.1 installed:
action#creativebox-45198:~$ gem install rails
Successfully installed rails-4.0.1
1 gem installed
action#creativebox-45198:~$ rails -v
-bash: rails: command not found
Should be fine running gem install rails.
This issue has been resolved through Nitrous support channels. Somehow the file /home/action/.rvm/gems/ruby-2.0.0-p247/bin/rails was missing, and gem install rails did not add it. Manually adding this file with the following contents solved the issue.
#!/usr/bin/env ruby_noexec_wrapper
#
# This file was generated by RubyGems.
#
# The application 'railties' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/
version = $1
ARGV.shift
end
end
gem 'railties', version
load Gem.bin_path('railties', 'rails', version)
Try hash rails if you're using Bash, or rehash rails for csh, at the command-line.
Sometimes the shell loses track of where files are and (re)hash tells the shell to figure it out.
Do a search for hash + shell or use man hash for more information.