ruby

How to write ruby code easier?(I mean in terminal write and then run it)

When I write a little ruby code, after a little bit, I always need to create a new terminal tab to ruby it, to see if it's correct. Are there any ways to do it in one window? Like a vim plugin or some other tool? ...

how do I call a method of an ruby object by giving a string as the method name

Hi folks, it's about Ruby. I've got a Box Object with attributes such as "panel1", "panel2", ..., "panel5". Instead of calling Box.panel1, Box.panel2, ... I want to call it like Box.method_call("panel" + some_integer.to_s). I'm sure there is a way like this, but how's the correct way? Yours, Joern. ...

JRuby mysterious stack trace

I'm using JRuby 1.4.0 complete jar in one of my projects. I'm executing it from ant via the exec task: java -jar jruby1.4.0.jar script.rb script_params The thing is, on some computers I receive this: [exec] Exception in thread "main" :1: no !/ in spec (IOError) [exec] ...internal jruby stack elided... [exec] from (unknown).(unk...

How to have an optional local variable in a partial template in rails?

I was thinking that at the top of my partial I would have something like this <% optional_width = default_value unless (defined? optional_width) But I've had inconsistent results with this, I'm thinking this is not a good way to do this. What is the "correct" way to do this in rails? ...

Save after delegation

When using the delegation feature of Ruby in Models: class A def foo ... end def foo=(arg) ... end end class B < A belongs_to :a delegate :foo, :foo=, :to => :a end How can I force B to save A after setting b.foo="what_ever"? Oh and a.save should be called only if b.save is called and b.foo= was used - just ...

How Create Post Request with redirect like a Form Submit

How can in controller redirect with post same data like this do form <form action="http://example.com/" method="POST"> <input type="hidden" name="q" value="a"> </form> I can't use redirect_to, because there is no support post data. ...

How do I make a ruby gem available to my scripts?

On my web host's system, installing and using gems seems to require installing my own copy of ruby. With their help on a forum, I've gotten that done. I'm trying to use a particular gem (called Image Science) in a Rails app. At this point, if I open irb and type the following, this is what I get: require 'rubygems' #true require 'image...

How to get 'è' (and not 'e') with activerecord and ruby 1.8.7

Hello folks, I am writing a simple script to update a table data. I am unable to get a record trough a field named "Agliè"; the problem is "è". c = Comune.find_by_denominazione_italiano_tedesco('Agliè') I realised that the problem can be patched using "Aglie", but I need to preserve the accent difference (these are town names, some a...

What does map(&:name) do in this Ruby code?

Trying to understand Ruby a bit better, I ran into this code surfing the Internet: require 'rubygems' require 'activeresource' ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/events.log") class Event < ActiveResource::Base self.site = "http://localhost:3000" end events = Event.find(:all) puts events.map(&:name...

How do I get ruby gems to include all dependencies of a gem, even ones in BETA?

Ruby gems only seems to include non-beta dependencies. eg. try: gem install ripple --include-dependencies : and you get the message: ERROR: Error installing ripple: ripple requires activesupport (~> 3.0.0.beta, runtime) Update: I found that this isn't possible in ruby gems. This however finally worked. Thanks @levi: gem updat...

ruby - get a file from directory without listing all contents

I'm using the split linux command to split huge xml files into node-sized ones. The problem is now I have directory with hundreds of thousands of files. I want a way to get a file from the directory (to pass to another process for import into our database) without needing to list everything in it. Is this how Dir.foreach already works?...

Slow SSL Connection on SSL + LDAP

Hi guys, I have installed Ruby on Rails and am using Webbrick server. My Apache Server re-routes from port 80 to port 443 to have a SSL connection and LDAP. After the LDAP Authentication, MY SSL Page loads very slowly, sometimes, it just dosent load at all, sometimes blank page instead of project home page. IF I cancell the redirect...

Convert an integer into a signed string in Ruby

I have a report in which I'm listing total values and then changes in parentheses. E.g.: Songs: 45 (+10 from last week) So I want to print the integer 10 as "+10" and -10 as "-10" Right now I'm doing (song_change >= 0 ? '+' : '') + song_change.to_s Is there a better way? ...

Heroku and Yahoo! Apps

Hi All, I have created an App on Heroku which works perfectly in a Web Browser. As a second step, I want to view the App hosted on Heroku in Yahoo! App. Its pretty simple to open any webpage in a Yahoo! App. One just need to mention the URL where the site is LIVE and rest is taken care by Yahoo! App. However, if I try to open the App h...

What is the behavior of ruby Hash#merge when used with a block

It does not seem to be documented very much: hsh.merge(other_hash){|key, oldval, newval| block} → a_hash http://ruby-doc.org/core/classes/Hash.html#M002880 ...

GSM-7 conversion- and septet-encoding library in Ruby?

I am looking for a pure Ruby solution to convert UTF-8 to GSM-7 and back, and do septet encoding/decoding along the way. Background here is: Sending and receiving SMS via a gateway and via REST-requests. I found a solution with libiconv (http://mobiletidings.com/2009/07/06/gsm-7-encoding-gnu-libiconv/) (which works more or less, but is...

Ruby multithreading / multiprocessing readings.

Can anyone recommend any good multithreading / processing books / sites which go into detail about the intricacies of Ruby multithreading / multiprocessing? I tried using ruby threading and basically in deadlock-free code on 1.9vm it ran into deadlocks in jruby. Yes I realize the differences are drastic (jruby has no GIL) but I wanted t...

Saving output of a query onto a text file in Ruby

I'm trying to query a table, fetch all records, and save the result as a CSV file. This is what I've done so far: require 'OCI8' conn = OCI8.new('scott','tiger','020') file = File.open('output.csv','w') do |f| conn.exec('select * from emp') do |e| f.write log.join(',') end end .. And while it does generate a CSV file, the proble...

Sorting a tree structure by folders first in Ruby

I have an array of paths, array = [ 'a.txt', 'b/a.txt', 'a/a.txt', 'a/z/a.txt' ] I need to create a tree structure (for the jTree plugin), but it has to be sorted by folders first (alphabetically) and then leafs (alphabetically too). A sorted tree structure with the above example would look like this: a z a.txt a.txt b a.txt...

Ruby: After inserting arrays into a list, how can I retrieve them and process them as arrays?

Here's what I'm trying to do: val1 = [26, 27, 24, 25, 29, 28] val2 = [17, 20, 22, 21] val3 = [36, 33, 31, 29] val4 = [20, 18, 17, 22, 21, 23] vals = {val1, val2, val3, val4} sum = 0 count = 0 vals.each do |val| for i in 0..val.size-1 do #sum += val[i] p val[i] ++count end end puts sum puts count Initially I wanted t...