undefined method `model=' for Qt::ListView - ruby

I am trying to write an application with Ruby and Qt. I have the following code:
list = Qt::ListView.new(window)
mod1 = MyModel.new #MyModel inherits from Qt::AbstractListModel
list.model = mod1 #<< Fails on this line
But it fails, telling me:
undefined method `model=' for #<Qt::ListView:0x0000000067e300>
Yet I see other posts on SO that use model=, and I see the method listed in IRB when I issue list.public_methods.

OK, the problem is that I wasn't calling super() in the initialize method of my model.

Related

What is the method called when the URI('http://google.com') start?

Almost all of you used URI module to convert a url string to an object in order to make some validation or change.
Example:
require 'uri'
URI('https://google.com')
# => #<URI::HTTPS https://google.com>
As you can see, the result is the HTTPS object under the URI module.
So, there is a question what is run when you write module/class name with round braces like the line of code above.
I thought, it is implicit calling of call method, but I got NoMethodError.
Example:
class MyClass
def self.call
puts 'You were right!'
end
end
MyClass()
# => NoMethodError: undefined method `MyClass' for main:Object
Funny enough, with the code you've shown, MyClass.() works (which is an alias for .call).
However in the case of URI, this actually a method (methods can begin with capitals). You can see the source code here: https://apidock.com/ruby/Kernel/URI/instance

NoMethodError but did 'require_relative'

I did the 'requir_relative' but still got the NoMethodError.
There are 2 ruby files, under 'run.rb' I have this
class Run
def separate(data)
hash_block = []
(0...data.count).each do |i|
f = data[i].split('|')
hash_block[i] = Hashing.new(f[0].to_i, f[1], f[2], f[3], f[4])
end
hash_block
end
end
and then in the main file, I did these:
require_relative 'run'
...some codes...
to_separate = IO.readlines(ARGV[0])
separated = separate(to_separate)
...some codes...
but I still get this:
in `block in <main>': undefined method `separate' for main:Object (NoMethodError)
If I cut the method and paste it in the main file it will work as expected but that is something I wanted to avoid.
In order to call the method within the class Run you have to instantiate it. Since is an instance method. The way your calling the class is giving you the error undefined because it can not find it with in the scope of your current file
run_instance = Run.new
to_separate = IO.readlines(ARGV[0])
sperated = run_instance.separate(to_separate)
You required the file, but in that file you have a class definition. separate is inside that class (and that's an instance method), so you need an object to call the method on.
separated = Run.new.separate(to_separate)

undefined method constantize in rake task

I'm trying to run this code"
FACTORY = %w(ProcessedTransaction Chargeback).freeze
FACTORY.constantize.each do |factory|
factory.public_send(:delete_all)
end
end
But I get this error: NoMethodError: undefined methodconstantize' for #`
Do you know how I can solve the issue?
In ruby uppercased variables are constants by default so you can't call constantize on it. In your case it's just an array so this should work:
FACTORY = %w(ProcessedTransaction Chargeback).freeze
FACTORY.each do |factory|
factory.constantize.public_send(:delete_all)
end
You can call String#constantize only on strings but you are calling it on array FACTORY.
Remove FACTORY.constantize and add factory.constantize.public_send(:delete_all)
Also make sure you have ActiveSupport::Inflector required

Strange behavior with embedded ruby

We are observing a strange behavior on our embedded ruby application. We have stripped down the code to bare minimal and were able to re-produce the issue. Following are the details.
1. The ruby code
#!/usr/bin/env ruby
#require "MyLibrary.so" *// Works fine*
module AA
class BC
def initialize
end
def loadFunction
require "MyLibrary.so" *//Gives error*
end
end
end
#Invoke the method
AA::BC.new().loadFunction
2. Source code for MyLibrary.so
#include "ruby.h"
const char loop[] =
"def loopFunc\n"
"puts \"HERE\"\n"
"end\n"
"begin\n"
"loopFunc()\n"
"rescue StandardError\n"
"puts $!\n"
"puts $!.class\n"
"end\n";
void initialize()
{
ruby_init();
ruby_init_loadpath();
rb_eval_string(loop);
}
extern "C" void Init_MyLibrary()
{
initialize();
}
When we require "MyLibrary.so" file inside the loadFunction in rb file, we are getting following error
undefined method `loopFunc' for main:Object
NoMethodError
However when we require at top of the rb file everything works fine.
Our first guess was that rb_eval_string() gets executed inside the module AA. So loopFunc is getting defined inside the module AA instead of being global. Therefore NoMethodError is being reported. When we invoked AA::BC.new().loopFunc() inside the cpp file, the method gets invoked successfully; which confirmed our guess.
Is this the expected behavior from an embedded ruby point of view because if we require an rb file(instead of .so) with the same code as passed to rb_eval_string, we are not getting any error.
rb_eval_string() does define the methods in the module from which it is invoked. We can either use rb_require/rb_load to get the correct behavior.

Ruby: Include a dynamic module name

I have a situation in my Rails application where I need to include arbitrary modules depending on the current runtime state. The module provides custom application code that is only needed when certain conditions are true. Basically, I'm pulling the name of a company from the current context and using that as the filename for the module and its definition:
p = self.user.company.subdomain + ".rb"
if File.exists?(Rails.root + "lib/" + p)
include self.class.const_get(self.user.company.subdomain.capitalize.to_sym)
self.custom_add_url
end
My test module looks like this:
module Companyx
def custom_add_url
puts "Calling custom_add_url"
end
end
Now in the console, this actually works fine. I can pull a user and include the module like so:
[1] pry(main)> c = Card.find_by_personal_url("username")
[2] pry(main)> include c.class.const_get(c.user.company.subdomain.capitalize)=> Object
[3] pry(main)> c.custom_add_url
Calling custom_add_url
If I try to run the include line from my model, I get
NoMethodError: undefined method `include' for #<Card:0x007f91f9094fb0>
Can anyone suggest why the include statement would work on the console, but not in my model code?
I'm doing a similar thing. I found this answer useful:
How to convert a string to a constant in Ruby?
Turns out I was looking for the constantize method. This is the line I'm using in my code:
include "ModuleName::#{var.attr}".constantize
Edit:
So ack, I ran into various problems with actually using that line myself. Partially because I was trying to call it inside a method in a class. But since I'm only calling one method in the class (which calls/runs everything else) the final working version I have now is
"ModuleName::#{var.attr}".constantize.new.methodname
Obviously methodname is an instance method, so you could get rid of the new if yours is a class method.
Include is a method on a class.
If you want to call it inside a model, you need to execute the code in the context of its singleton class.
p = self.user.company.subdomain + ".rb"
if File.exists?(Rails.root + "lib/" + p)
myself = self
class_eval do
include self.const_get(myself.user.company.subdomain.capitalize.to_sym)
end
self.custom_add_url
EDIT:
class << self doesn't accept a block; class_eval does, hence it preserves the state of local variables. I've modified my solution to use it.

Resources