ruby

Stop duplicates from being added to an array of Ruby objects

how can I eliminate duplicate elements from an array of ruby objects using an attribute of the object to match identical objects. with an array of basic types I can use a set.. eg. array_list = [1, 3, 4 5, 6, 6] array_list.to_set => [1, 2, 3, 4, 5, 6] can I adapt this technique to work with object attributes? thanks ...

Approaching Java from a Ruby perspective

There are plenty of resources available to a Java developer for getting a jump-start into Ruby/Rails development. The reverse doesn't appear to be true. What resources would you suggest for getting up-to-date on the current state of java technologies? How about learning how to approach DRY (don't repeat yourself) without the use of meta...

How Do I Create an 'OR' Condition Using the ActiveRecord Model

Given the following code which creates an and condition, how do I make it create an or condition instead? Country.first(:conditions=>{:media_code=>country_code, :code=>country_code}) ...

Active Record like functionality on array instance variable

I would like to write a module that provides active record like functionality on an array instance variable. Examples of its use would be x = Container.new x.include(ContainerModule) x.elements << Element.new x.elements.find id module ContainerModule def initialize(*args) @elements = [] class << @elements def <<(e...

What does :this means in Ruby on Rails?

Hi, I'm new to the Ruby and Ruby on Rails world. I've read some guides, but i've some trouble with the following syntax. I think that the usage of :condition syntax is used in Ruby to define a class attribute with some kind of accessor, like: class Sample attr_accessor :condition end that implicitly declares the getter and setter f...

How to interface Ruby with Mercurial (Python)?

I'm hoping to integrate Mercurial into my Rails site. I want to run basic commands like status, summary, log, and maybe even clone. I'd like to interface directly with the Mercurial libraries and avoid doing any command line parsing via regex. Mercurial is written in Python; I am using Ruby. So, I need Mercurial Ruby bindings. It seems,...

Is it possible to generate plain-old XML using Haml?

I've been working on a piece of software where I need to generate a custom XML file to send back to a client application. The current solutions on Ruby/Rails world for generating XML files are slow, at best. Using builder or event Nokogiri, while have a nice syntax and are maintainable solutions, they consume too much time and processing...

Ruby TCPSocket read_all

Is there a method that can act like read_all, as in instead of using TCPSocket.read(no_of_bytes), one can simply TCPSocket.read_all. I am sending objects first by YAML::dump'ing them then sending them but I don't know a way to get their size in bytes. Thanks in advance, ell. Oh and I am very, very new to any form of network programming s...

Structuring the UI code of a single-page EXTjs Web app using Rails?

I’m in the process of creating a large single-page web-app using ext-js for the UI components with Rails on the backend. I’ve come to good solutions for transferring data using Whorm gem and Rails support of RESTful Resources. What I haven’t come to a conclusion on is how to structure the UI and business logic aspects of the applicatio...

How can I delete a file in Sinatra after it has been sent via send_file?

I have a simple sinatra application that needs to generate a file (via an external process), send that file to the browser, and finally, delete the file from the filesystem. Something along these lines: class MyApp < Sinatra::Base get '/generate-file' do # calls out to an external process, # and returns the path to the gene...

How to get a list of IPs for a remote computer in Ruby

I have a app that needs to connect over a socket to a remote computer that now has multiple IP addresses. Is there a way to turn the remote Hostname or IP a list of all the IPs that the system has? Possibly (Hostname | Ip) => (RemoteMAC) => IPs? These will be windows server 2003/2008 machines. ...

Why is "#{String}" a common idiom in Ruby

A Ruby dev I know asked this; my answer is below... Are there other, better reasons? Why do so many Ruby programmers do "#{string}" rather than string since the second form is simpler and more efficient? ...

Albacore msbuild task problem

Just updated albacore to version 0.14 and ran into a major problem. My current environment is: Ruby 1.9.1 Rake 0.8.7 Albacore 0.1.4 The problem is that as of now all my rake build throw a funny little exception: undefined method 'push' for #<Enumerator:0x???????> So far I have traced the problem to albacore msbuild.rb line 38 and...

Weird Haml 3 error with ruby 1.9.1 and rails 3

I'm getting this weird error on my windows 7 computer when I am using the html2haml command with Haml 3 and Rails on Ruby 1.9: -- control frame ---------- c:0017 p:-9593720 s:0052 b:0052 l:000051 d:000051 TOP c:0016 p:---- s:0050 b:0050 l:000049 d:000049 CFUNC :require c:0015 p:0026 s:0046 b:0046 l:000045 d:000045 TOP C:/Ruby/lib/ru...

Can a rake task know about the other tasks in the invocation chain?

Rake (like make) is able to have many targets/tasks specified on invocation. Is it possible for a rake task to access the list of tasks the user invoked, in order to do its job? Scenario: Consider a Rake-based build tool. A help task would like to know what tasks were also specified in order to print their usage and halt the build proc...

In Ruby, can the coerce() method know what operator it is that requires the help to coerce?

In Ruby, it seems that a lot of coerce() help can be done by def coerce(something) [self, something] end that's is, when 3 + rational is needed, Fixnum 3 doesn't know how to handle adding a Rational, so it asks Rational#coerce for help by calling rational.coerce(3), and this coerce instance method will tell the caller: # I know ...

After passing a reference to an method, any mods using that reference are not visible outside the method - Ruby?

I am passing the reference of name to *mod_name*, I modify the referenced object from within the method but the change is not visible outside of the method, if I am referring to the same object from all locations how come the value is different depending on where I reference it? name = "Jason" puts name.object_id #19827274 def mo...

What's the shortest way to generate a random IP address in Ruby?

Just like the title says, wanted for testing purposes. Thanks! ...

To check whether a digit is inside an integer (ruby)

Chinese people do not like numbers with a digit 4 in it. I am going to implement a membership program with membership numbers not including the digit 4, say: number = 3 number.next.has4? => true how the has4? method can be done (efficiently)? ** EDIT Thanks for the answers, I performed simple benchmarking as a reference: class...

is the ruby mysqlplus adapter production ready

is the ruby mysqlplus adapter production ready? have a choice between: 1. mysql library by tmtm (has a pure ruby and c version) 2. mysqlplus by http://github.com/oldmoe/mysqlplus/ 3. dataobjects (used my datamapper ORM) 4. em-mysql Currently using the C version of the mysql gem, that is the oldest and most popular option. Evented...