ruby

Including Files in Ruby Questions

I am very new to Ruby so could you please suggest the best practice to separating files and including them. What is the preferred design structure of the file layout. When do you decide to separate the algorithm into a new file? When do you use load to include other files and when do you use require? And is there a performance hit whe...

Programming ruby from a web browser

_why enables you to run ruby (irb in particular) from a web browser, and github allows you to edit code that's under version control from a web browser. Is there anything that allows you to do both, ie edit code and run it, from a web browser? ...

ruby: how to fire and forget a subprocess?

Hi, I have a long running process and I need it to launch another process (that will run for a good while too). I need to only start it, and then completely forget about it. I managed to do what I needed by scooping some code from the Programming Ruby book, but I'd like to find the best/right way, and understand what is going on. Here'...

Getting a NameError with ActiveRecord and relationships

I've run into a problem when using a one to many relationship. I want to have each Series have one Publisher and that one Publisher has many Series. This is my Publisher model: class Publisher < ActiveRecord::Base validates_presence_of :name has_many :series end This is my Serie model: class Serie < ActiveRecord::Base belongs_...

Ruby: How do I invoke a function via an object reference?

Consider this contrived example: # Dispatch on value of fruit_kind: TYPE_A = :apple TYPE_B = :banana TYPE_C = :cherry eating_method = nil case fruit_kind # Methods to use for different kinds of fruit (assume these are # already defined) when TYPE_A then eating_method = bite when TYPE_B then eating_method = peel when TYPE_C...

Error compiling Ruby 1.8.7 Build 160

I was compiling Ruby 1.8.7's latest version yesterday (since the latest version has no binaries out yet). I followed the instructions in the readme file, but then, when I installed ruby gems and update it, it displays the error that zlib.so cannot be found. I am pretty sure that I already downloaded zlib libraries, etc. I also investig...

Ruby: How to detect when one side of a socket has been closed

How can I detect that a socket is half-open? The case I'm dealing with is when the other side of a socket has sent a FIN and the Ruby app has ACKed that FIN. Is there a way for me to tell that the socket is in this condition? Take, for example: require 'socket' s = TCPServer.new('0.0.0.0', 5010) loop do c = s.accept until c.cl...

How do I create a .p12 file in Ruby?

I've looked at the Ruby OpenSSL documentation, but I can't quite figure out the pieces I need to put together to make a .p12 file. There's also this tutorial, but the comments belie an ambivalence about its correctness. ...

Ruby: How do I use symbols to represent things in an array?

I have an array of arrays that looks like this: fruits_and_calories = [ ["apple", 100], ["banana", 200], ["kumquat", 225], ["orange", 90] ] I also have a method I want to invoke on each element of the array: fruits_and_calories.each do |f| eat(f[0], f[1]) I'd really like to be able to say something like: fruits_and_calorie...

How can I run Rails "script/[xyz]" commands on Windows?

I am used to doing Rails development work on a Mac. However, now other developers will be working with me and they use Windows, both XP and Vista. I am trying to figure out the Windows environment so I can help them. In OS X and Linux you have this kind of thing... $ cd [Rails project root] $ script/console Tried it on Windows but al...

sorting a ruby array of objects by an attribute that could be nil

Hi All, I have an array of objects that I need to sort by a position attribute that could be an integer or nil, and I need the objects that have the nil position to be at the end of the array. Now, I can force the position to return some value rather than nil so that the array.sort doesn't fail, but if I use 0 as this default, then it p...

How can I convert a Blowfish encoded binary string to ASCII in Ruby?

I would like to encode some plain text using Ruby and the Crypt library. I would like to then transmit this encrypted text (along with some other data) as an ASCII hexadecimal string within an XML file. I have the following code snippet: require 'rubygems' require 'crypt/blowfish' plain = "This is the plain text" puts plain blowfish...

debugging a rails controller making too many queries

Hi All, I have a rails application that about 3 years old, and I'm having a problem with my pages making too many queries. Every page that loads has several lines that look like this: ReqTdsLink Columns (1.0ms) SHOW FIELDS FROM `req_tds_links` what sort of method call would cause this output in the log? I can't find any before fil...

how to optimise site load times in ruby?

I'm a newbie who is creating a lightweight photo showcase site written on the cake php framework with RoR.i plan to use effects from the scriptalicious library, as well as jquery for photo display & transition effects. As the site will be very photo-rich, what programming steps can i take to ensure that all photos and other pages load q...

How do I declare NaN (not a number) in Ruby?

Also "NaN".to_f returns 0 instead of NaN. ...

Unable to install rmagick in Windows Machine

Hi All, I am new to rmagick and ruby. I am using ruby 1.8.6 version Actually i downloaded rmagic2.9.0 win32 version and i unzipped it and i installed imageMagic-6.4.8. Then from command prompt i gave gem update --system. C:>gem update --system Updating RubyGems Nothing to update then C:>gem install rmagick --local ERROR: could not...

Using Apache and mod_ext_filter, need to dynamically replace values of static file based off of query string.

I've got a situation where I need to alter the contents of a cached file based off of one of the query string arguments passed in. I'd love to use sed to do a simple regular expression replacement of a value based off of said argument but I can't figure that one out. I could use a ruby script to do the replacement for me but can't seem...

Unit conversion causing problem when data returned to the browser after form submit -- Ruby on Rails

Background, Part 1 I have a form that collects both frequency and duration from the user. We set both the frequency and the duration in seconds, but, to the user, they are always presented as days or weeks. For example, the user sees "every other day", but the value set on the option is '172800' as that is the number of seconds in 2 days...

When is it better to use a Struct rather than a Hash in Ruby?

A Ruby Struct allows an instance to be generated with a set of accessors: # Create a structure named by its constant Customer = Struct.new(:name, :address) #=> Customer Customer.new("Dave", "123 Main") #=> #<Customer name="Dave", address="123 Main"> This looks convenient and powerful, however, a Hash does something prett...

Ruby optional parameters

If I define a Ruby functions like this: def ldap_get ( base_dn, filter, scope=LDAP::LDAP_SCOPE_SUBTREE, attrs=nil ) How can I call it supplying only the first 2 and the last args? Why isn't something like ldap_get( base_dn, filter, , X) possible or if it is possible, how can it be done? Thanks for any replies! ...