Disable editing of QTableView in rubyqt - ruby

I'm trying to disable editing of QTableView in rubyqt. It's supposed to be done by setting triggers to QAbstractView::NoEdiTriggers:
TableView.setEditTriggers(QAbstractView::NoEditTriggers);
The trouble is, rubyqt doesn't recognize Qt::AbstractView:
irb(main):008:0> require 'Qt4'
=> true
irb(main):009:0> Qt::AbstractView
NameError: uninitialized constant Qt::AbstractView
from (irb):9:in `const_missing'
from (irb):9
from /usr/bin/irb:12:in `<main>'
Is there another way to disable editing with ruby and qt?
EDIT
Oh, and outside of irb:
searcher.rb:72:in `const_missing': uninitialized constant Qt::AbstractView (NameError)
And searcher.rb:72:
#ui.tableView.setEditTriggers(Qt::AbstractView::NoEditTriggers)
Changing it to (Qt::AbstractView.NoEditTriggers) doesn't work, neither.

require 'Qt4'
Qt::Application.new(ARGV) do
Qt::Widget.new do
self.window_title = 'Hello QtRuby v1.0'
resize(200, 100)
button = Qt::PushButton.new('Quit') do
connect(SIGNAL :clicked) { Qt::Application.instance.quit }
end
tv = Qt::TableView.new do
setEditTriggers(Qt::TableView::NoEditTriggers)
end
tm = Qt::StandardItemModel.new(1, 1) do
setItem(0,0,Qt::StandardItem.new("aaa"))
end
tv.setModel tm
self.layout = Qt::VBoxLayout.new do
add_widget(tv, 0, Qt::AlignRight)
add_widget(button, 0, Qt::AlignCenter)
end
show
end
exec
end
The main idea is that if no Abstract class from Qt is binded to Ruby, try looking for it's ancestors or implementations.

Related

Dependency Injection causing both Rspec failure and IRB failure

Note: I am a Ruby and programming novice.
I have a class called JourneyLog I am trying to get a method called start to instantiate a new instance of another class, called Journey
class JourneyLog
attr_reader :journey_class
def initialize(journey_class: Journey)
#journey_class = journey_class
#journeys = []
end
def start(station)
journey_class.new(entry_station: station)
end
end
When I go into irbi get the following issue
2.2.3 :001 > require './lib/journeylog'
=> true
2.2.3 :002 > journeylog = JourneyLog.new
NameError: uninitialized constant JourneyLog::Journey
from /Users/BartJudge/Desktop/Makers_2018/oystercard-challenge/lib/journeylog.rb:4:in `initialize'
from (irb):2:in `new'
from (irb):2
from /Users/BartJudge/.rvm/rubies/ruby-2.2.3/bin/irb:15:in `<main>'
2.2.3 :003 >
I also have the following Rspec test
require 'journeylog'
describe JourneyLog do
let(:journey) { double :journey, entry_station: nil, complete?: false, fare: 1}
let(:station) { double :station }
let(:journey_class) { double :journey_class, new: journey }
describe '#start' do
it 'starts a journey' do
expect(journey_class).to receive(:new).with(entry_station: station)
subject.start(station)
end
end
end
I get the following Rspec failure;
1) JourneyLog#start starts a journey
Failure/Error: expect(journey_class).to receive(:new).with(entry_station: station)
(Double :journey_class).new({:entry_station=>#<Double :station>})
expected: 1 time with arguments: ({:entry_station=>#<Double :station>})
received: 0 times
# ./spec/jorneylog_spec.rb:9:in `block (3 levels) in <top (required)>'
I am at a total loss on what the problem is, or where to look for some answers.
I'm assuming I'm not injecting the Journey class properly, but thats as far as I can get myself.
Could someone provide some assistance?
In the journeylog.rb file you need to load the Journey class:
require 'journey' # I guess the Journey class is defined in lib/journey.rb
In the spec file you need to pass journey_class to the JourneyLog constructor:
describe JourneyLog do
subject { described_class.new(journey_class: journey_class) }
# ...

undefined method 'execute' for nil:NilClass

I am making a tool in ruby which can interact with databases.
I am using amalgalite as an adapter for sqlite3.
Code:
require 'amalgalite'
# this is class RQuery
class RQuery
def db_open(db_name)
#db = Amalgalite::Database.new "#{db_name}.db"
make_class
end
def exec_this(query)
#db.execute(query)
end
def make_class
tables_list = exec_this("select name from sqlite_master where type='table'")
tables_list.each do |table|
#class_created = Object.const_set(table[0].capitalize, Class.new)
#class_created.class_eval do
define_singleton_method :first do
RQuery.new.exec_this("select * from #{table[0]} order by #{table[0]}.id ASC limit 1")
end
end
end
end
def eval_this(input)
instance_eval(input)
end
def code
print '>>'
input = gets
exit if input =~ /^q$/
puts eval_this(input)
code
end
end
Now when I am running the code everything works fine until I call table_name.first
It gives output
vbhv#fsociety ~/git/R-Query/bin $ ruby main.rb
Enter the code or q for quit
>>db_open('vbhv')
users
persons
people
programmers
>>Users.first
/home/vbhv/git/R-Query/lib/r-query.rb:36:in `instance_eval': undefined method `execute' for nil:NilClass (NoMethodError)
Did you mean? exec
from /home/vbhv/git/R-Query/lib/r-query.rb:29:in `block (3 levels) in make_class'
from (eval):1:in `eval_this'
from /home/vbhv/git/R-Query/lib/r-query.rb:36:in `instance_eval'
from /home/vbhv/git/R-Query/lib/r-query.rb:36:in `eval_this'
from /home/vbhv/git/R-Query/lib/r-query.rb:43:in `code'
from /home/vbhv/git/R-Query/lib/r-query.rb:44:in `code'
from /home/vbhv/git/R-Query/lib/r-query.rb:44:in `code'
from main.rb:4:in `<main>'
Now the 'execute' function it is talking about is inside amalgalite. What am I doing wrong here?? Thanks in Advance!
The problem in this was that the new class formed dynamically doesn't know about the connection variable '#db'. Hence the code solves the problem.
#class_created.instance_variable_set(:#database, #db)
A big thanks to Jagdeep Singh.

Why does Ruby hang when I use method_missing on String to redirect to chars? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When I implement this code:
class String
def method_missing(meth,*args, &block)
if self.chars.respond_to? meth
self.chars.send meth, *args, &block
else
super
end
end
def respond_to?(meth)
if self.chars.respond_to? meth
true
else
super
end
end
end
Ruby gets stuck on flatten. Even requiring another file internally calls flatten so it hangs there. I've even tried this:
class String
def method_missing(meth,*args, &block)
if meth.to_sym == :flatten
super
elsif self.chars.respond_to? meth
self.chars.send meth, *args, &block
else
super
end
end
def respond_to?(meth)
if meth.to_sym == :flatten
super
elsif self.chars.respond_to? meth
true
else
super
end
end
end
But the same results occur. What's happening internally to cause flatten to fail?
Here's the error output:
2.1.2 :003 > require 'mygem'
=> true
2.1.2 :004 > require 'pry'
^CIRB::Abort: abort then interrupt!
from /home/user/dev/MyGem/lib/mygem/string_method_missing.rb:17:in `call'
from /home/user/dev/MyGem/lib/mygem/string_method_missing.rb:17:in `respond_to?'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/requirement.rb:112:in `flatten'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/requirement.rb:112:in `initialize'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/requirement.rb:70:in `new'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/requirement.rb:70:in `default'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/dependency.rb:260:in `merge'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/specification.rb:1323:in `block in activate_dependencies'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/specification.rb:1306:in `each'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/specification.rb:1306:in `activate_dependencies'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/specification.rb:1288:in `activate'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems.rb:194:in `try_activate'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:132:in `rescue in require'
from /home/user/.rvm/rubies/ruby-2.1.2/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:144:in `require'
from (irb):4
from /home/user/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
It happens strictly for any Array of strings which gets flattened.
2.1.2 :005 > [1,2,3].flatten
=> [1, 2, 3]
2.1.2 :006 > ["1","2","3"].flatten
^CIRB::Abort: abort then interrupt!
from /home/user/dev/MyGem/lib/mygem/string_method_missing.rb:17:in `call'
from /home/user/dev/MyGem/lib/mygem/string_method_missing.rb:17:in `respond_to?'
from (irb):6:in `flatten'
from (irb):6
from /home/user/.rvm/rubies/ruby-2.1.2/bin/irb:11:in `<main>'
UPDATE
I've updated the code for the fixes recommended in the comments. The problem is still happening. I'm looking forward to the answer.
Flatten seems to work if I tell it how deep to go. So that seems to indicate an infinite loop on flattening the array on the string object. Example:
2.1.2 :013 > ["1","2","3"].flatten(0)
=> ["1", "2", "3"]
2.1.2 :021 > ["1","2","3"].flatten(8)
=> ["1", "2", "3"]
2.1.2 :022 > ["1","2","3"].flatten(9)
=> ["1", "2", "3"]
How can I avoid the infinite loop and still do this method_missing? It would help to know what test flatten calls on the inner object so I could define that in the string and avoid the loop.
There isn't an inbuilt cap on flatten. So redirecting everything to chars in String through method_missing creates an Array like object that will cause the flatten method to infinitely loop.
Add this, it is a working solution:
class Array
# To fix a bug that our method_missing creates
# we need to set a MAXIMUM for FLATTEN
alias_method :_old_flatten, :flatten
alias_method :_old_flatten!, :flatten!
def flatten(level = 99)
_old_flatten(level)
end
def flatten!(level = 99)
_old_flatten!(level)
end
end

Ruby: CLI progress bar with open-uri

I'm trying to experiment with the open-uri and want to make a Command line interface progress bar.
I've going over the documentation for OpenURI::OpenRead where is has a progress bar code sample.
pbar = nil
open('latest.zip', 'wb') do |fo|
fo.print open('http://wordpress.org/latest.zip',
:content_length_proc => lambda { |t|
if t && 0 < t
pbar = ProgressBar.new("...", t)
pbar.file_transfer_mode
end
},
:progress_proc => lambda {|s|
pbar.set s if pbar
}).read
end
but I'm can keep getting the following error:
zip_dowloader.rb:11:in `block (2 levels) in <main>': uninitialized constant ProgressBar (NameError)
gem install progressbar
Then add:
require 'progressbar'
to the top of your script.
Or, in a bundler-enabled project, add:
gem 'progressbar'
to your Gemfile and run bundle install.

TypeError: superclass mismatch for class Word in Ruby

I am creating a Word class and I am getting an error:
TypeError: superclass mismatch for class Word
Here is the irb code:
irb(main):016:0> class Word
irb(main):017:1> def palindrome?(string)
irb(main):018:2> string == string.reverse
irb(main):019:2> end
irb(main):020:1> end
=> nil
irb(main):021:0> w = Word.new
=> #<Word:0x4a8d970>
irb(main):022:0> w.palindrome?("foobar")
=> false
irb(main):023:0> w.palindrome?("level")
=> true
irb(main):024:0> class Word < String
irb(main):025:1> def palindrome?
irb(main):026:2> self == self.reverse
irb(main):027:2> end
irb(main):028:1> end
TypeError: superclass mismatch for class Word
from (irb):24
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from C:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
A thumb rule for irb (either way irb or rails console)
If you are creating the same class twice with inheritance (superclass), exit the irb instance and create it again. Why this? Because otherwise class conflicts will happen.
In your case, you are using Windows (found from the question), so just type exit on DOS prompt and again type irb or rails console and create your Word class and it should work. Please let me know if it doesn't work for you.
The reason it gives you a superclass mismatch error is because you have already defined the Word class as inheriting from Object
class Word
...
end
In Ruby (like in most dynamic languages) you can monkey-patch classes by reopening the definition and modifying the class. However, in your instance, when you are reopening the class you are also attempting to redefine the class as inheriting from the super class String.
class Word < String
...
end
Once a class and it's inheritance structure have been defined, you cannot define it again.
As a few people have said, exiting and restarting irb will allow you to start from scratch in defining the Word class.
link664 has clearly explained the problem.
However, there's an easier fix without quitting irb (and losing all your other work).
You can delete an existing class definition this way.
irb(main):051:0> Object.send(:remove_const, :Word)
and you can verify with:
irb(main):052:0> Word.public_instance_methods
which should return:
NameError: uninitialized constant Word
from (irb):52
An easy way to bypass this issue is to encapsulate both classes between different modules:
> module M
> class Word
> def palindrome?(string)
> string == string.reverse
> end
> end
> end
=> nil
> w = M::Word.new
=> #<Word:0x4a8d970>
> w.palindrome?("foobar")
=> false
> w.palindrome?("level")
=> true
> module N
> class Word < String
> def palindrome?
> self == self.reverse
> end
> end
> end
> N::Word.new("kayak").palindrome?
=> true

Resources