ruby

How to call an object's method from within a different class in Ruby?

Sorry about the wording of the question, I don't really know how to put it. What I mean is, how could I do this: class Test def doSomething puts "I'm working" end end class numberTwo def doSomethingElse subject.doSomething puts "Doing other things" end end subject = Test.new otherObject = numberTwo.new otherObject.d...

Twitter OAuth Request Token Using Grackle

I am currently trying to use the Grackle Ruby GEM to integrate with the Twitter API, but I have encountered a little snag. I am attempting to perform a GET to twitter.com/oauth/request_token, but according to the OAuth spec I need to provide the following values: oauth_consumer_key oauth_signature_method oauth_signature oauth_timestam...

Sending data between ruby and objective-C

I'm trying to serialize data for sharing between Ruby and Objective-C. I would like to use something lightweight like YAML or JSON, but I would be curious to hear what would be the easiest implementation. Thanks. ...

How can I set variables in Ruby with an if statement on one line?

With Ruby, how can I set a variable based on a condition? Something like: dog_name = params[:dog][:name] if params[:dog] And then dog_name just wouldn't be set if params[:dog] wasn't set. Right now I'm getting an error if params[:dog] is nil. ...

ruby recursive looping fails

asdf = [[1,2,3],[11,22,33,44,55,66],[51]] def recursive(params, index) if (index==params.size) puts "DONE" end currentParam = params[index] currentParam.each do |sh| puts sh recursive(params, index+1) end end recursive(asdf,0) I was expecting an output like: 1 11 22 33 44 55 66 51 2 11 22 33 44 55 66 51 3 11 22 33 44 55...

testing to see how many instances of ruby script i can run

is it true that it is better to run multiple instances of ruby scripts than doing multiple threads for ruby < 1.9 ? I would like some tools or approaches to measure how much bandwith, cpu, memory is being used by each instances of ruby script (ex. a web spider). The goal here is trying to see the maximum number of ruby scripts that ca...

Trying to find a count of items from a two level deep association, any ideas?

I am working on an application where I need to find the count of submitted items by users that have been referred by a user. For Example - User1 has referred 3 people (User2, User3, User4) and each of those users has submitted 5 articles. I am trying to find a way to get the count of submitted items in User1's tree (should be 15 in th...

MySQL Rails migration error: "Error on rename of schema_migration (errno: -1)"

Hi there, I'm a PHP dev and I'm new to Rails but have been getting on pretty well, everything seems pretty straightforward. However, up until this morning I have been using SQLite and decided to move what I'm building to MySQL. rake db:create works perfectly, but when I attempt to rake db:migrate I get the following error: rake abort...

Can't install gemcutter as a Ruby Gem source

I'm trying to install the thinking sphinx gem, which is on gemcutter.org When I run the command gem sources -a http://gemcutter.org I get the error: Error fetching http://gemcutter.org: bad response Not Found 404 (http://gemcutter.org/specs.4.8) If I change the above URL to http://gemcutter.org.specs.4.8.gz it finds a file. How do...

Ruby includes returns false and nil

Can someone explain what the difference between false and nil this case is: irb(main):008:0> Fixnum < Integer => true irb(main):011:0> Integer < Fixnum => false irb(main):012:0> String < Numeric => nil I realize that "strings are not numbers" and that "not all integers are fixnums" My thinking is naive and boolean. Either something i...

Extending classes and instances

This question has two parts. In the Ruby Programming Language book, there is an example (section 8.1.1) of extending a string object and class with a module. First question. Why is it that if you extend a class with a new method, and then create an object/instance of that class, you cannot access that method? irb(main):001:0> module G...

Hash problem in Ruby

Hi, I am newbie in Ruby.. seeking some help.. I have a code DB = { 'key1' => "value1", 'key2' => "value2"} key = gets DB["#{key}"] when i enter key1 from console i get nil how to resolve this? I tried few different alternative but could not solve it. hope to get help here. Thanks ...

Threading in Ruby with a limit

I have a task I need to perform, do_stuff(opts), that will take ~1s each, even while 1 - 10 of them are running in parallel. I need to collect an array of the results for each operation at the end. If I have 30 stuffs to do, how would I use threading effectively to queue up the do_stuff(opts) operations so no more than 10 are running co...

Ruby, Sinatra and Closing Connections

Does anyone know if there is a way to prevent sinatra from sending the 'Connection: close' header in its responses? To be clear, I have a very simple get '/path' do puts "Some (~200 byte long) string" end But on putting the output through a network analyser I see its sending the Connection: close header straight after the HTTP/1.1 ...

Detecting that a method was not overridden

Say, I have the following 2 classes: class A def a_method end end class B < A end Is it possible to detect from within (an instance of) class B that method a_method is only defined in the superclass, thus not being overridden in B? Update: the solution While I have marked the answer of Chuck as "accepted", later Paolo Perrota m...

A better way to conditionally display info in views?

I find myself frequently writing code like this in the HTML HEAD and other places: <% if @canonical_url %> <link rel="canonical" href="<%= @canonical_url %>"/> <% end %> I then set the variable in the controller if it's appropriate. Is there any way of writing the equivalent on one line, or maybe a different way of organizing the c...

Advice/tools for working on a large existing rails application?

I recently joined a new company with a large existing codebase. Most of my experience has been in developing small-medium sized apps which I was involved with since the beginning. Does anyone have any useful tools or advice on how to begin working on a large app? Do I start at the models and move to controllers? Are there scripts to vi...

How can I improve a Ruby on Rails code that has a lot of SQL as strings?

I have a piece of Ruby on Rails code that has a complex SQL query (well, not that complex, but as far as I know beyond the ORM capabilities) and for my taste it has too many strings and harcoded values. I'd like to improve it as much as possible, so my question is open ended, what else can I do to improve it? Some particular issues I ha...

Where to put common code found in multiple models?

I have two models that contain the same method: def foo # do something end Where should I put this? I know common code goes in the lib directory in a Rails app. But if I put it in a new class in lib called 'Foo', and I need to add its functionality to both of my ActiveRecord models, do I do that like this: class A < ActiveRecord:...

Ruby, Sinatra and Streaming

I'm furiously trying to debug why iTunes isn't accepting the data I'm sending to it (as a DAAP Server) - you can see what I'm trying to do on the github page. The only difference I can find between my response and an identical response from mt-daapd (which iTunes does accept) is that the mt-daapd response is broken down into small packe...