ruby

rails : what's wrong with this multiple join with conditions on the associations?

Here are my models: class Deck < ActiveRecord::Base belongs_to :game has_many :deck_cards end class DeckCard < ActiveRecord::Base belongs_to :card belongs_to :deck end class Card < ActiveRecord::Base end Here's my attempted find: DeckCard.all :joins => [:card, :deck], :conditions => {{:decks => {:game_id => @game.id}}, {:ca...

Ruby: Length of a line of a file in bytes?

I'm writing this little HelloWorld as a followup to this and the numbers do not add up filename = "testThis.txt" total_bytes = 0 file = File.new(filename, "r") file.each do |line| total_bytes += line.unpack("U*").length end puts "original size #{File.size(filename)}" puts "Total bytes #{total_bytes}" The result is not the same as th...

Efficient update of MySQL table from SQL Server

We have a MySQL database (mostly read-only, so MyISAM tables) living in the data center that talks to a SQL Server DB living on-site. There is some significant latency in the WAN (upwards of 100ms); in about 6 months the SQL Server DBMS will be moving up to the data center (e.g. same gigabit LAN). In the MySQL DB, I have several thousan...

How do I include a module to a class such that the module overrides the class?

Is there a way to include a module to a class such that the module's methods override the methods of the class? For example: module UpcasedName def name @name.upcase end end class User attr_accessor :name include UpcasedName end u = User.new u.name = 'john' puts u.name # outputs 'john', not 'JOHN' In the example above, u...

Rake string replace in file

Hi, Does Rake have anything built in for replacing strings inside files and such or is it best to use bash commands inside 'sh', or use Ruby's own file manipulation functionality? Regards, Chris ...

Ruby: Posting XML to RESTFUL Web Service using Net::HTTP::Post

I am having trouble getting this to work so any help would be appreciated! Basically, the request.body contains valid XML for the Web Service like so: <somedata> <name>Test Name 1</name> <description>Some data for Unit testing</description> </somedata> ...but the service returns empty XML, like so (note that the id field is returned s...

How do you override the ruby case equality operator? (===)

Hi, I have a class that I want to compare to both strings and symbols in a case statement, so I thought that I just override the ===() method for my class and all would be gold. However my ===() method never gets called during the case statement. Any ideas? Here is some example code, and what happens in a irb session: class A def i...

How to script the java debugger command-line tool (jdb)?

How can I drive the debugging session with some scripting language like ruby? Is there any easier options than using Expect or some similar module with a scripting language? ...

soap4r custom headers

I've been working with soap4r and trying to use the SOAP::Header::SimpleHandler, I'm trying to get it to put a custom header on the outgoing message, but I can't work out how to get it to include attributes rather than as subelements: class ServiceContext < SOAP::Header::SimpleHandler NAMESPACE = "http://context.core.datamodel.fs....

Where can I find a collection Of Ruby Gems?

Where can I find a collection of free available Ruby gems? Why reinvent the wheel if a gem is already available that you can re-use? ...

Connect to specific VisualStudio instance via COM from Ruby

How can I attach and automate a specific instance of the VisualStudio.DTE from Ruby? It works fine when only one instance is running, but when multiple instances are running, WIN32OLE.connect("VisualStudio.DTE.9.0") only connects to the first one launched. A non-Ruby specific approach would also be appreciated. According to this KB art...

Removing a slot on a child button click

I've been trying to remove a slot from a child button click, but I can't seem to get it to work. E.G. flow do button("X") {parent.remove} end Any suggestions? ...

How to write 'if' without using 'then' or 'end' in Ruby

I've found three ways to write the same condition in Ruby: #1 if 1==1 puts "true" end #2 puts "true" if 1==1 #3 if 1==1 then puts "true" end Why can't I do this? #4 if 1==1 puts "true" I don't understand: (1) Why then and end are needed in #3, and, (2) Why I need to change the order to get #2 to work. Statement #4 seems...

How can I define sections without === Headings for RDoc::usage()?

I like to generate man pages using '--help' output via help2man and txt2man. Ruby's RDoc system is very convenient, but I can't seem to configure RDoc::usage in exactly the way that I want. Here's a sample script: #!/usr/bin/env ruby # # === Name # # foobar - Example command for StackOverflow question # # === Synopsis # # Usage: fo...

GServer receive messages

Hi, I use the code below to create a TCP/IP server and then the client code to call it but for some reason the server doesn't receive the message sent by the client. Please advise. Thanks Server: class MyServer < GServer def initialize(port=20607, host=GServer::DEFAULT_HOST) super(port, host, Float::MAX, $stderr, true) e...

How can I avoid running ActiveRecord callbacks?

I have some models that have after_save callbacks. Usually that's fine, but in some situations, like when creating development data, I want to save the models without having the callbacks run. Is there a simple way to do that? Something akin to... Person#save( :run_callbacks => false ) or Person#save_without_callbacks I looked in t...

What's the best way to search for a string in a file?

Hi, The title speaks for itself really. I only want to know if it exists, not where it is. Is there a one liner to achieve this? Regards, Chris ...

What's the difference between a helper and a partial?

Functionally speaking, both seem like subroutines that generate HTML based on certain parameters. Is the decision to use one or the other in a given circumstance purely aesthetic? ...

Accessing the app name from inside a rails template when generating rails app

I'm messing around with rails 2.3 templates and want to be able to use the app name as a variable inside my template, so when I use... rails appname -m path/to/template.rb ...I want to be able to access appname inside template.rb. Anyone know how to do this? Thanks ...

GServer Thread Problems In Ruby

Hi, Looking around I found http://www.devco.net/archives/2008/06/26/adventures_with_ruby.php where the blogger moan about GServer's bug to destory threads and reaching its maximum connections. Is this problem real / still true? Thanks ...