ruby

Best way to write this calculation in ruby.

amt = self.alt_inv - (self.alt_tax ? self.alt_tax : 0) - (self.alt_freight ? self.alt_freight : 0) - (self.misc1_amt ? self.misc1_amt : 0) - (self.misc2_amt ? self.misc2_amt : 0) ...

How to find all cycles of a String in Ruby ?

Hi, I have written a method in Ruby to find all the circular combination of a text x = "ABCDE" (x.length).times do puts x x = x[1..x.length] + x[0].chr end Is there a better way to implement this ? ...

Is there any Hijri Date library in Ruby?

Is there any Hijri Date library in Ruby? ...

using xpath on single Nokogiri node returns elements in all nodes

I am parsing an XML doc that looks something like this: <MyBook> <title>Favorite Poems</title> <issn>123-456</issn> <pages>45</pages> </MyBook> <MyBook> <title>Chocolate Desserts</title> <issn>654-098</issn> <pages>100</pages> </MyBook> <MyBook> <title>Jabberwocky</title> <issn>454-545</issn> <pages>19</pages>...

Rails 3 ActiveModel::Serializers seem to need lots of support methods

I'm returning to RoR after not using it for a few years and I'm trying to use ActiveModel to serialise a plain object to XML. I'm doing the following, as per the comments in activemodel/lib/activemodel/serialization.rb: class XmlError include ActiveModel::Serializers::Xml attr_accessor :code attr_accessor :description def at...

How to get odd occurring text in a String in Ruby

Hi, I have a String and I want to get another string out of it which has only characters at odd occuring positions. For example if i have a string called ABCDEFGH, the output I expect is ACEG since the character indexes are at 0,2,4,6 respectively. I did it using a loop, but there should be one line implementation in Ruby (perhaps usin...

Rails - Dynamically defining instance methods in a model

I'm not sure if this can even be achieved, but here goes... :) Lets assume two models, a Page model and a Field model. A Page has_many :fields and the Field model has two attributes: :name, :value What I want to achieve is in the Page model to dynamically define instance methods for each Field#name returning the Field#value. So if my ...

Best way to emulate Ruby "splat" operator in PHP function signatures [Method overloading]

In Ruby def my_func(foo,bar,*zim) [foo, bar, zim].collect(&:inspect) end puts my_func(1,2,3,4,5) # 1 # 2 # [3, 4, 5] In PHP (5.3) function my_func($foo, $bar, ... ){ #... } What's the best way to to do this in PHP? ...

Cheat sheet for all design patterns implemented in Ruby?

I wonder if there are cheat cheets for all design patterns implemented in Ruby so that you don't have to reinvent the wheel. ...

Ruby Gem Ultraviolet, Rails, Leopard

I cannot get this crazy dependency to work and please don't tell me to go to Snow Leopard because I have a PPC. I have oniguruma, textpow, ultraviolet all installed. I'm using rvm which is probably the problem. Not that rvm is bad it's just that I don't know how to get the paths correct. When I startup a rails app the error I'm getting...

Auto-completion in Textmate for Ruby?

I'm really used to auto-completion coming from Netbeans. In Netbeans, when I type a 'string' and then hit a 'dot' it will print out a list of methods for the String class. TextMate doesn't seem to have that function. Is it something you could add? Would save A LOT of time instead of using the ri/irb/online doc all the time. ...

form_for is giving me the wrong url for path_prefixes

Hi I am having a problem with the form_for method in Rails. It is behaving strangely. I have a route with a path prefix, something like: map.resources :beers, :path_prefix => '/:brewery' And I have a form like this (@beer.brewery is a string, just the name of the brewery): <% form_for @beer, :url => { :brewery => @beer.brewery } d...

How to order by multi relation table 's field?

Hi , I need to generate a list of receipts in rails which need to be ordered by item's order relationship field (payment_method_meta_type.name). Models : Receipt Deposit PaymentMethodMetaType In Deposit Model: class Deposit < ActiveRecord::Base belongs_to :payment_method_meta_type has_many :receipts, :class_name=>"Receipt", ...

what does the empty regex match in ruby?

following a RoR security tutorial (here), i wrote something along the lines of @@private_re = // def secure? action_name =~ @@private_re end the idea is that in the base case, this shouldn't match anything, and return nil. problem is that it doesn't. i've worked around for the time being by using a nonsensical string, but i'd like t...

Rotating a section of a two dimensional array.

I've read this SO question on how to rotate a two-dimensional array many times, and I was curious as to how you could expand this situation to work with a section of a two-dimensional array. I've thought about it for a while, and I can't seem to come up with a good equation. Essentially what I'm wanting to do is something like this: ...

anybody have the ruby GUI tools ——shoe box?

Ps: I don't want to use tk ...

How would you handle a mixed HAML/ERB team?

I'm a big Haml/Sass fan. Right now I work with a designer who prefers Erb and doesn't speak Haml. I find that working with Erb is slowing me down considerably, and it doesn't seem fair that he should have to pay for that time (he's the client & I'm paid hourly). The erb->haml converter works quite well these days, but I don't know if t...

How to connect Ruby Cygwin environment to with a SQL Server 2008 database?

I run a Windows XP machine, and use Cygwin to run my ruby environment. I usually connect to a MySQL database, but for purposes of testing the application when using a SQL Server database I need to setup a connection to my SQL Server database. Does anyone know how to setup my environment to connect to SQL Server? ...

accessing remote xml in rails

I need to access xml that is located in another web server. Does rails/ruby have a function that allows me to access this xml and then may be store it in a variable so that i can process it with libxml-ruby? ...

Tell Netbeans it's a Ruby file.

I've got a file named Vagrantfile and it's a ruby file but it cannot have the .rb at the end of the filename. Is there a way to tell Netbeans that it's Ruby file? ...