ruby

Datamapper's hooks won't work

Can't understand why hooks don't work. I have the following model: class DirItem include DataMapper::Resource # property <name>, <type> property :id, Serial property :dir_cat_id, Integer, :required => true property :title, String, :required => true property :price, Integer, :default => 0 belongs_to :dir_cat has n, :dir...

Capitalize first letter in ruby

upcase method capitalizes the entire string. I need to capitalize only the first letter. Also, i need support several popular languages, like "german", "russian" etc. How to do it ? ...

yard and links to classes/modules in the doc

Hi, I'm currently switching from rdoc to yard for my ruby software documentaion. In my doc I often reference some classes/modules from the comments, for instance : ## == Provides various utility features ## ## == Features ## ## Logging : logging is provided by the Mysoft::Mypackage::Utility::Logger class ## Rdoc correctly creates a...

is there a nicer oneliner for handling "unless somthing.nil? || something[:key].nil?"

Is there a way to make this one liner better looking? @var = params[:key1][:key2] unless params.blank? || params[:key1].blank? ...

Provisioning database schemas in ruby

I'm looking for a database agnostic way to create new schemas. Something like postgres_uri = "postgres://username:password@hostname/database" mysql_uri = "mysql://username:password@hostname/database" [postgres_uri, mysql_uri].each do |db| connection = DB.connect(db_uri) connection.create_schema("xyz") end Bonus marks for somethin...

Ruby's tap idiom in Python

There is a useful Ruby idiom that uses tap which allows you to create an object, do some operations on it and return it (I use a list here only as an example, my real code is more involved): def foo [].tap do |a| b = 1 + 2 # ... and some more processing, maybe some logging, etc. a << b end end >> foo => [1] With Rails...

Better data structure for multiple checks for a variable than if statements?

So, I have this block of code, and yes it looks ugly. Is there a better a data structure I can use to compare row.add,row.modify,row.delete and row.query with "Y" and call the get_role function? Note this block of code runs in a loop, hence the 'row'. if row.add == "y" role_ids << get_role("c") end if row.modify == ...

Identify which action is being called inside a model. Ruby on Rails

How do I come to know whether a create or any other action has been called from inside a model. Basically I am doing database logging and want to track whether a create or other actions are being performed. For doing the logging part I am using the concept of ActiveRecord::Observer. But there I am not able to find out whether the user i...

how to if-else in one line in ruby?

Hi all. I want to use if-else condition in one line. I have used the ternary operator, but doesn't seem to work. Any clues? class Array def painful_injection each do |item| sum = yield (defined?(sum).nil?) ? 0 : sum, item #pass the arguments to the block end sum end end puts [1, 2, 3, 4].painful_injection {|sum, nxt_i...

How do I use multiple "one-to-many" nested attributes in form_for

given the fact that a user has many credit cards and a credit card has many addresses, I am trying to create a form that creates a user and credit card with address all at once relavent model code: class User < ActiveRecord::Base has_many :credit_cards accepts_nested_attributes_for :credit_cards end class CreditCard < ActiveRecord...

HTTP client to post XML RAW_DATA to restful API

Hey I am trying to find out how to send an XML in string as RAW_DATA to a RESTful API. I found out a couple of libraries (Nestful, Restfulclient, curb) but wasn't able to send the content properly as you'd send it from Rails Functional tests @request.env['RAW_POST_DATA'] = xml.to_s Any suggestions? ...

What is the right syntax for Ruby to do a system call to curl?

To refresh Redmine, I need SVN to ping our Redmine installation from our post-commit hook. Our post-commit hook is a Ruby script that generates an email. I'd like to insert a call do this: curl --insecure https://redmineserver+webappkey This call works from the command line but when I try to do this: #!/usr/bin/ruby -w REFRESH_DRADIS...

"Magic constructor" in Ruby for all attributes

Is there a way to set a default initialize method without writing it down? class DataClass attr_accessor :title, :description, :childs def hasChilds? @childs.nil? end end I Want to initialize this class with standard initial attributes. Something like this: $> a = DataClass.new(:title => "adsf", :description => "test") $> a...

Multi-line input problems when using STDIN.gets

Having looked at this question, I have the following code: $/ = "\0" answer = STDIN.gets Now, I was hoping that this would allow the user to: enter a multi-line input, terminating by pressing Ctrl-D. enter a single line input, terminating by pressing Ctrl-D. enter a "nothing" input, terminating by pressing Ctrl-D. However, the beh...

how to Clear or replace contents of input field using selenium?

Hi How can i clear the contents of an input field using ruby selenium? i.e when page loads up there are values in the input filed but i want to replace them with new ones. thank you ...

Rails 3 on Fedora issues

Hi, I have been trying to get Rails 3 installed in Fedora, it requires Ruby to be 1.8.7 or 1.9.2. Fedora official yum repos have Ruby 1.8.6 which is of no use. So I decided to compile it myself from the source, downloaded the source, compiled it and installed it successfully. But this weird thing happens after the install, sudo gem or ...

Sum the value of array in hash.

This is my array [{:amount=>10, :gl_acct_id=>1, :alt_amount=>20}, {:amount=>20, :gl_acct_id=>2 , :alt_amount=>30}] i want result [{:amount => 30}] or {:amount = 30} Any idea? ...

Array of indexes to array of ranges

Ranges in ruby are pretty cool. I end up with arrays such as this: geneRanges = [(234..25), (500..510), (1640..1653)] And subsequently have to remove bits of them. For that I: genePositions = geneRanges.collect {|range| range.entries }.flatten => [500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 1640, 1641, 1642, 1643, 1644, ...

How to build my own rack preview like sinatra - staticmatic

Hi Guys I have HAML SASS converter and want to allow users the localhost:30000 functionality i guess to view their HAML SASS files in the browsers. Any guides you know that might help me? Thanks ...

How do I update a sequential attribute with foobar#{n} with n incrementing ?

I want to write basically User.find(:all).each {|u| u.update_attribute("email","nil#{incrementing_number}")} How do I make that #{incrementing_number} incremement per updated attribute. :D ...