ruby

Custom threads?

How does one create custom threads to run in a Gtk application? Given this simplistic example: @w = Gtk::Window.new "testtest" @l = Gtk::Label.new "test" @w.add @l @w.show_all Gtk.main How could I run a thread like this? Thread.start { loop { puts 'thread running'; @l.text = Time.now.to_s; sleep 1 }} I got that timeout-based appro...

writing to the middle of a file in ruby

I've opened a file in ruby with the options a+. I can seek to the middle of the file and read from it but when I try to write the writes always go to the end. How do I write to a position in the middle? jpg = File.new("/tmp/bot.jpg", "a+") jpg.seek 24 puts jpg.getc.chr jpg.seek 24 jpg.write "R" jpg.seek 28 jpg.write "W" puts jpg.pos j...

Rails 2.0.2 App on Snow Leopard?

I've got an app developed on Ruby 1.8.6 and frozen to use Rails 2.0.2 that hits problem after problem on Snow Leopard: -Default Snow Leopard Ruby 1.8.7 64-bit and 32-bit running Rails 2.0.2 -rake tasks not seeing ZenTest -openssl header mismatches while compiling 32-bit Ruby 1.8.6 from source -image_science apparently requiring Xcode De...

Determine if a value exists in an array of hashes

I have the following: array_of_hashes = [{:a=>10, :b=>20}, {:a=>11, :b=>21}, {:a=>13, :b=>23}] How would I go about finding if :a=>11 exists in array_of_hashes array_of_hashes.include? does not seem to work ...

possible to convert ruby script to exe so that source code not visible ?

possible to convert ruby script to exe so that source code not visible ? ...

select parent node containing text inside children's node.

basically i want to select a node (div) in which it's children node's(h1,b,h3) contain specified text. <html> <div id="contents"> <p> <h1> Child text 1</h1> <b> Child text 2 </b> ... </p> <h3> Child text 3 </h3> </div> i am expecting, /html/div/ not /html/div/h1 i have this below, but unfortunately returns the children, instead of th...

how to get text between : and string ?

i have a bunch of texts in an array ["name: hi", "pw: lol"] so i get each array. How can i just extract hi and lol ? ...

Factory methods in Ruby

What is the slickest, most Ruby-like way to have a single constructor return an object of the appropriate type? To be more specific, here's a dummy example: say I have two classes Bike and Car which subclass Vehicle. I want this: Vehicle.new('mountain bike') # returns Bike.new('mountain bike') Vehicle.new('ferrari') # returns C...

Problem in understanding how singleton methods work in Ruby

I am struggling to understand how singleton methods work in Ruby at the object level. When I define a simple Person class and add singleton method and instance methods and try to access eigenclass object id of that object it returns different ids. To put simply here is my test code. class Person attr_accessor :someAccessor def met...

Conditional summarizing via inject

How to get the index of item in: my_array.inject {|rs,item| rs += item} I need to summarize all except the i-th element. ...

Implementing page controller all over rails

Hi All. I seem to have got a little a head of myself. I have created a Page Model and Pages controller. The whole idea was to be able to call something like 'print :controller=>'Pages', :action=>'view', :id=>'6', :layout=>'none'' And in my application.html.erb I have a div with yield, and in the next div I would like to have the above...

Running code after class is fully loaded

class A do_something_from_b def method_in_a end end module B def self.included base base.extend ClassMethods end module ClassMethods def do_something_from_b A.class_eval do alias_method :aliased_method_in_a, :method_in_a end end end end A.send(:include, B) That code will fail because wh...

Why do migrations need the table block param?

Why does the ruby on rails migration syntax look like this: create_table :my_table do |t| t.integer :col t.integer :col2 t.integer :col3 end And not: create_table :my_table do integer :col integer :col2 integer :col3 end Personally I find the second snippet much more readable, are there any reasons w...

xml diff in ruby?

What is the best/fastest way to merge two xml documents with ruby? I have two xml files, one that's formatted so it is visually appealing, one that isn't (and it has comments and whitespaces stripped) that has a few changes to some of the nodes throughout, and it gets changed often. So I'm trying to figure out a simple and efficient so...

How to get current context name of rspec?

if i have rspec like this describe 'Foo' do // init go here describe 'Sub-Foo' do it "should Bar" do // test go here // puts ... <-- need "Foo.Sub-Foo should Bar" here end end end How can i get "Foo.Sub-Foo should Bar" inside the test context at // test go here? It is similar to format with spe...

Visitor Pattern in Ruby, or just use a Block?

Hey there, I have read the few posts here on when/how to use the visitor pattern, and some articles/chapters on it, and it makes sense if you are traversing an AST and it is highly structured, and you want to encapsulate the logic into a separate "visitor" object, etc. But with Ruby, it seems like overkill because you could just use blo...

How to distribute ruby software in debian environments

How to easily package software that uses multiple gems into a debian installable packages? ...

what does "?" in ruby means ?

I want to know it real meaning of it and how to use it exactly. Another question is about assert I saw assert product.valid? product.errors.full_messages and assert product.valid? But I can't find syntax for those assert what does second arg for assert (product.errors.full_messages) means or it is arg for ? Thanks ...

Extend model in plugin with "has_many" using a module

I have a some code in an engine style plugin which includes some models. In my app I want to extend one of these models. I have managed to add both instance and class methods to the model in question by including a module from within an initializer. However I cannot seem to add associations, callbacks etc. I get a 'method not found' er...

get_eval of Selenium::Client::Driver is not working in Internet Explorer 8 browser.

Hi, I am working on ruby + selenium for automation testing of web application. I have added the following in the 'env.rb' @browser = Selenium::Client::Driver.new(c['server_host'], c['server_port'], browser_type, c['root_url'], c['timeout']) I am able to execute the javascript code in "Mozilla 3" using following code. total_count =...