ruby

Rails: is it a good idea to store the view in a global var?

I'm thinking of storing the view, what i mean by view is like this: app/views/profiles/index.html.haml into this i would do $view = self then in other parts of my app i would use and edit the $view var. it might get troubles? ...

Skip HTML escape in custom label_tag helper in Rail 3

Hi, I have this nice class ErrorFormBuilder that allows me to add the error description near the corresponding field in the form view : class ErrorFormBuilder < ActionView::Helpers::FormBuilder #Adds error message directly inline to a form label #Accepts all the options normall passed to form.label as well as: # :hide_errors...

Is it possible in Ruby to define a method which name ends with : (colon)?

Just wondering if it is possible, by some loophole, to define a method name that ends in a colon. The purpose it to make things look like this: mymethod: arg1,arg2,arg3 ...

How do I temporarily monkey with a global module constant?

Greetings, I want to tinker with the global memcache object, and I found the following problems. Cache is a constant Cache is a module I only want to modify the behavior of Cache globally for a small section of code for a possible major performance gain. Since Cache is a module, I can't re-assign it, or encapsulate it. I Would Lik...

Rails cookies not working right

Rails is setting and returning a remember_token cookie like this: = cookies[:remember_token] = value6c69b17681d2bf316f8eexpiresThu Jun 10 14:55:00 -0400 2010 In other words, when I'm calling the cookie I don't just get the value. Why is this? I am setting the cookie as follows: cookies[:remember_token] = { :value => @user.remember_...

ruby hash to object - Parsing data from JSON object

Hi all, I'm just starting to dabble in consuming a JSON webservice, and I am having a little trouble working out the best way to get to the actual data elements. I am receiving a response which has been converted into a ruby hash using the JSON.parse method. The hash looks like this: {"response"=>{"code"=>2002, "payload"=>{"topic"=>[...

alias_method and class_methods don't mix?

Greetings, I've been trying to tinker with a global Cache module, but I can't figure out why this isn't working. Does anyone have any suggestions? This is the error produced for the below code: NameError: undefined method get' for moduleCache' from (irb):21:in `alias_method' module Cache def self.get puts "original" ...

How do you assign a variable with the result of a if..else block?

I had an argument with a colleague about the best way to assign a variable in an if..else block. His orignal code was : @products = if params[:category] Category.find(params[:category]).products else Product.all end I rewrote it this way : if params[:category] @products = Category.find(params[:category]).products else @produc...

Why doesn't this simple Ruby program print what I expect it to?

I have this: require 'tempfile' t = Tempfile.new('test-data') t.open t.sync = true t << "apples" t.puts "bananas" puts "contents are [#{t.read}] (#{t.size} bytes)" t.close This prints: contents are [] (14 bytes) Why aren't the contents actually shown? I'm on Ruby 1.9.2. ...

rails: running a method on create only

I want to run a paperclip method on create only has_attached_file :file This method doesn't seem to accept the :on => :create that some other rails methods do. I tried: before_create after_create etc, but those didn't work. I also did: if :create How can I test if the controller is using the create method from the model? Than...

rails: checking which controller method was called from within the model

Is there a way to check which controller method was called from within the model? Example: Say the controller create method was called: def create do something end Then in the model do something only when create in the controller was called if create? do something end ...

access following entry in a form_for, and switch a value between two entry of my DB

I am displaying a list of articles. I sort my articles by the param order and I want when displaying the list of article to be able to "move" them up or down. In php I do everything with a for browsing my array of results and inside the for I go to the next index to find where I am in the list, and with which other article I must swap m...

How to specify custom Sass directory with sinatra

Instead of serving my Sass files from the default 'views' directory I'd like to change this to /assets/sass The following attempts are in my main ruby root file in the app: Attempt 1 set :sass, Proc.new { File.join(root, "assets/sass") } get '/stylesheet.css' do sass :core end With this I get the following error: myapp.rb:17 ...

Trying to set the message-id, in-reply-to, etc... in ActionMailer

I'm working on an app that needs to be able to send out email updates and then route the reply back to the original item. All emails will come to a single address (this can't change unfortunately), and I need to be able to determine where they go. My initial thought was setting the message-id for the item so that it comes back as a Ref...

Kernel.Select from ruby causing Thread(0x7f1a159abd58): deadlock (fatal)

I don't have multiple threads or anything like that, and I thought select() was non-blocking, however, as I add more items to an array used as the IO object, and using select(), after about 1000~ items in the array (with some interaction on them).. the whole script exits with a deadlock / fatal error... Hope any1 could help Thank you so...

Passing arguments to an Rspec SpecTask

Rake allows for the following syntax: task :my_task, :arg1, :arg2 do |t, args| puts "Args were: #{args}" end I'd like to be able to do the same, but with RSpecs SpecTask. The following unfortunately fails: desc "Run example with argument" SpecTask.new('my_task'), :datafile do |t, args| t.spec_files = FileList['*_spec.rb -datafil...

Is this ruby code thread safe?

Is this code threadsafe? It seems like it should be, because @myvar will never be assigned from multiple threads (assuming block completes in < 1s). But do I need to be worried about a situation where the second block is trying to read @myvar as it's being written? require 'rubygems' require 'eventmachine' @myvar = Time.now.to_i Eve...

Looping differences in Ruby using Range vs. Times

I'm trying to solve a Project Euler problem using Ruby, I used 4 different looping methods, the for-loop, times, range and upto method, however the times method only produces the expected answer, while the for-loop, range and upto method does not. I'm assuming that they are somewhat the same, but I found out it's not. Can someone please ...

Advanced Rails Routing of short URLs and usernames off of root url

I want to have username URLs and Base 58 short URLs to resources both off of the root url like this: http://mydomain.com/username #=> goes to given user http://mydomain.com/a3x9 #=> goes to given story I am aware of the possibilities of a user names conflicting with short urls, and I have a workaround, but what I can't figure out is ...

fluent interface program in Ruby

we have made the following code and trying to run it. class Numeric def gram self end alias_method :grams, :gram def of(name) ingredient = Ingredient.new(name) ingredient.quantity=self return ingredient end end class Ingredient def initialize(n) @@name= n end def quantity=(o) @@quant...