undefined method `cache_getter' for Typhoeus::Hydra - ruby-on-rails

I don't know why I get this error. Both methods are corrects.
hydra = Typhoeus::Hydra.new
hydra.cache_getter do |request|
Rails.cache.read(request.cache_key) rescue nil
end
hydra.cache_setter do |request|
Rails.cache.write(request.cache_key,request.response, expires_in: request.cache_timeout)
end
ruby 1.9.2p320 (2012-04-20 revision 35421) [x86_64-darwin11.4.2]
Rails 3.2.8

You are getting that error because those methods do not exist on an instance of Typhoeus::Hydra. You can find the full list of available methods at http://rubydoc.info/gems/typhoeus/0.5.0/frames
Update
Those methods were removed between versions 0.4.2 and 0.5.0 via this commit

Related

solidus_searchkick - undefined method `deep_symbolize_keys'

I am using Solidus and I want to implement a better search wiht elasticsearch, I'm using solidus_searchkick to do that. But I get the following error:
undefined method 'deep_symbolize_keys' for #<ActionController::Parameters:0x0000556eae99cda8>
At Spree::HomeController#index, that has:
Spree::HomeController.class_eval do
def index
#searcher = build_searcher(params.merge(include_images: true))
#products = #searcher.retrieve_products
#taxonomies = Spree::Taxonomy.includes(root: :children)
end
end
I have NOT changed anything at Solidus' search options.
I get this error right after I run 'bundle install', installs everything just fine.
Then, I run 'rails s' and I get this error when I try to get my home or any other page that shows me any product.
Versions:
Rails: 5.1.6
Ruby: 2.5.1
solidus_searchkick: 0.3.4
Solidus: 2.5.0
This is an issue with the deprecated method deep_symbolize_keys in Rails 5.1. I just submitted a Pull Request for the solidus_searchkick gem. You can find it here https://github.com/elevatorup/solidus_searchkick/pull/6/files
If you point your solidus_searchkick gem to that branch it will work.

Why is `duplicable?` defined the way it is?

I come across this in Rails source code:
class Object
def duplicable?
true
end
end
class NilClass
begin
nil.dup
rescue TypeError
def duplicable?
false
end
end
end
With this code, even after dup is removed from an object, that object responds to duplicable? with true.
I think it can be rewritten to a simpler code like:
class Object
def duplicable?
repond_to?(:dup)
end
end
What is the merit of defining duplicable? using begin...rescue?
What is the merit of defining duplicable? using begin...rescue?
Ruby before 2.4 raised a TypeError when attempting to nil.dup:
$ rbenv local 2.3.0
$ ruby --version
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
$ ruby -e 'p nil.dup'
-e:1:in `dup': can't dup NilClass (TypeError)
from -e:1:in `<main>'
Starting with Ruby 2.4, nil.dup just returns itself:
$ rbenv local 2.4.0
$ ruby --version
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin15]
$ ruby -e 'p nil.dup'
nil
Putting the method definition inside rescue ensures that the method is only defined for Ruby versions which raise the TypeError.
I think it can be rewritten to a simpler code like: [...]
Simply checking whether the receiver responds to dup doesn't work, because nil – being an Object – does respond to dup, even in 2.3. The TypeError is (was) raised from within Object#dup:
VALUE rb_obj_dup(VALUE obj)
{
VALUE dup;
if (rb_special_const_p(obj)) {
rb_raise(rb_eTypeError, "can't dup %s", rb_obj_classname(obj));
}
// ...
}
nil responds to dup explicitly throwing the TypeError (which has, in turn, nothing to do with NoMethodError.) [Correction: had responded to dup before 2.4, credits go to #Stefan.]
NilClass.instance_method(:dup)
#⇒ #<UnboundMethod: NilClass(Kernel)#dup>
The goal is to respond to duplicable? with false unless NilClass#dup is overwritten by another monkey patcher in the city. [Correction: read “another monkey patcher” as “Matz” :)]

undefined method `valid_xml?'

I am new to ruby. I keep getting undefined method 'valid_xml'? What am I doing wrong?
require 'rexml/document'
include REXML
begin
good_xml = %{
<groceries>
<bread>Wheat</bread>
<bread>Quadrotriticale</bread>
</groceries>}
puts(good_xml)
valid_xml?(good_xml)
puts("good read")
rescue Exception => e
puts(e.message)
end
and here is the output
-bash-4.1$ ruby test.rb
<groceries>
<bread>Wheat</bread>
<bread>Quadrotriticale</bread>
</groceries>
undefined method `valid_xml?' for main:Object
Here is the ruby version I have
ruby 1.8.7 (2009-12-24 patchlevel 248) [x86_64-linux]
valid_xml? is not defined in REXML (and neither did you define it). You could define it like this:
def valid_xml?(xml)
REXML::Document.new(xml)
true
rescue REXML::ParseException
false
end

Syntactic Errors in project files only in my system

I am getting syntactic errors in ruby files in my system although these are not there in other systems.
The errors are in code like:
1) redirect_to :back, alert: exception.message
The syntactic error is in alert
2) load_and_authorize_resource only: [ :update, :destroy ]
The syntactic error is in only
3) render json: #reward.to_json
The syntactic error is in json
Like that there are number of errors.
I am getting rid of these errors by doing following changes to the above:
1) redirect_to :back, :alert => exception.message
2) load_and_authorize_resource :only => [ :update, :destroy ]
3) render :json => #reward.to_json
It seems that error is due to ruby version but I am not sure what's the proper reason is?
I need to do all such changes in every existing projects and is quite painful.
Does that imply that the projects are using old ruby syntax or does that imply that I have old ruby version installed?
My ruby version is ruby 1.9.2p180
Also if its ruby version problem than does the ruby upgrade to the latest version will affect the whole project and how to accomplish this easily ?
UPDATE:
I confirmed that my ruby version is ruby 1.9.2p180 because of the following:
D:\ruby_work>ruby -v
ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
D:\ruby_work>pik list
187: ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32]
* 192: ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
The asteric(*) above is indicating the version I am currently using.
The exact errors are for example in redirect_to :back, :alert => exception.message is-
, unexpected ':'
UPDATE:
Now I did -
D:\ruby_work>pik use 187
D:\ruby_work>pik list
* 187: ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32]
192: ruby 1.9.2p180 (2011-02-18) [i386-mingw32]
D:\ruby_work>ruby -v
ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32]
The error is still there. Now I suspect Is it really a ruby error because changing to older ruby version also showing the same syntactic error. The only difference is that now even changing from :key to key => is also showing the same error.
Older versions of Ruby didn't understand the hash notation of key: value, only :key => value, which 1.9+ understands.
I don't remember when the new notation was added, but, as you found, the fix to allow the code to run on older versions is to use the original notation.
In Ruby 1.8.7, this is the error I get using IRB:
irb(main):001:0> foo = {a:'b'}
SyntaxError: compile error
(irb):1: syntax error, unexpected tSYMBEG, expecting kDO or '{' or '('
foo = {a:'b'}
^
(irb):1: syntax error, unexpected '}', expecting $end
from (irb):1
irb(main):002:0> foo = {:a => 'b'}
=> {:a=>"b"}

Ruby: How to make a public static method?

In Java I might do:
public static void doSomething();
And then I can access the method statically without making an instance:
className.doSomething();
How can I do that in Ruby? this is my class and from my understanding self. makes the method static:
class Ask
def self.make_permalink(phrase)
phrase.strip.downcase.gsub! /\ +/, '-'
end
end
But when i try to call:
Ask.make_permalink("make a slug out of this line")
I get:
undefined method `make_permalink' for Ask:Class
Why is that if i haven't declared the method to be private?
Your given example is working very well
class Ask
def self.make_permalink(phrase)
phrase.strip.downcase.gsub! /\ +/, '-'
end
end
Ask.make_permalink("make a slug out of this line")
I tried in 1.8.7 and also in 1.9.3
Do you have a typo in you original script?
All the best
There is one more syntax which is has the benefit that you can add more static methods
class TestClass
# all methods in this block are static
class << self
def first_method
# body omitted
end
def second_method_etc
# body omitted
end
end
# more typing because of the self. but much clear that the method is static
def self.first_method
# body omitted
end
def self.second_method_etc
# body omitted
end
end
Here's my copy/paste of your code into IRB. Seems to work fine.
$ irb
1.8.7 :001 > class Ask
1.8.7 :002?>
1.8.7 :003 > def self.make_permalink(phrase)
1.8.7 :004?> phrase.strip.downcase.gsub! /\ +/, '-'
1.8.7 :005?> end
1.8.7 :006?>
1.8.7 :007 > end
=> nil
1.8.7 :008 > Ask.make_permalink("make a slug out of this line")
=> "make-a-slug-out-of-this-line"
Seems to work. Test it out in your irb as well, and see what results you're getting. I'm using 1.8.7 in this example, but I also tried it in a Ruby 1.9.3 session and it worked identically.
Are you using MRI as your Ruby implementation (not that I think that should make a difference in this case)?
In irb make a call to Ask.public_methods and make sure your method name is in the list. For example:
1.8.7 :008 > Ask.public_methods
=> [:make_permalink, :allocate, :new, :superclass, :freeze, :===,
...etc, etc.]
Since you also marked this as a ruby-on-rails question, if you want to troubleshoot the actual model in your app, you can of course use the rails console: (bundle exec rails c) and verify the publicness of the method in question.
I am using ruby 1.9.3 and the program is running smoothly in my irb as well.
1.9.3-p286 :001 > class Ask
1.9.3-p286 :002?> def self.make_permalink(phrase)
1.9.3-p286 :003?> phrase.strip.downcase.gsub! /\ +/, '-'
1.9.3-p286 :004?> end
1.9.3-p286 :005?> end
=> nil
1.9.3-p286 :006 > Ask.make_permalink("make a slug out of this line")
=> "make-a-slug-out-of-this-line"
It's also working in my test script. Nothing wrong with your given code.It's fine.

Resources