ruby

Automatically Map JSON Objects into Instance Variables in Ruby

I would like to be able to automatically parse JSON objects into instance variables. For example, with this JSON. require 'httparty' json = HTTParty.get('http://api.dribbble.com/players/simplebits') #=> {"shots_count":150,"twitter_screen_name":"simplebits","avatar_url":"http://dribbble.com/system/users/1/avatars/thumb/dancederholm-peek...

Constructing URL parameters in Rails 3

I am trying to use the following code to convert a hash of options (e.g. { :opt1 => 'a', :opt2 => 'b' } ) into parameters to be appended to a URL (e.g. example.com/page?opt1=a&opt2=b): ActionController::Routing::Route.new.build_query_string(options) where options is the hash. I get the following error: wrong number of arguments (0 fo...

Can I make ruby display method parameter values in a backtrace?

Using MRI 1.9 When an exception is raised that causes a backtrace to be printed, it would often be immensely easier to debug if the backtrace showed the receiver and values of method parameters as well as the method name. Is there any way of doing this? Think about situations such as passing a nil deep into library code that wasn't ex...

Adding an rails activerecord association within a loop

I want to add a has_many through association to a activerecord model class for each symbol in an array. for example PeopleOrganisation::ROLES.each do |role| has_many role.to_s.pluralize.to_sym, :through => :people_organisations, :source => :person, :conditions => "people_organisations.role = '#{role.to_s}'" do def << (ob...

Need help creating a string

I'm new to Ruby and need to take 10 character rfid token serial data (from arduino & parallax rfid reader) and convert it to a string. It doesn't look like anything is being added to string. I am getting the rfid token data in the terminal window when I scan the token if that helps. require 'rubygems' require 'serialport' #params...

Best Practice to abstract ActiveRecord model queries in Rails?

I'd like to extract out logic from the controllers to somewhere that it can be more DRY. What's the best way of handling something like the following in Rails? For instance, as opposed to having the query in the controllers, I could place it in the model: class SmoothieBlender < ActiveRecord::Base belongs_to :user def self.get_...

Can't run "gem list"!

I get an error when i run: vagrant@vagrantup:~$ sudo gem list ERROR: Loading command: list (LoadError) no such file to load -- zlib ERROR: While executing gem ... (NameError) uninitialized constant Gem::Commands::ListCommand Even if I don't use sudo I get the same error message. It's on Ubuntu 10.4 and I have installed zlib...

What's the best way to start a background process, that can get accessed later on

Hi, I am currently developing a RubyGem that provides an executable. The executable keeps track of the state of some log files using the FSSM gem. This executable should get started, do something in background, and get stopped later on. For example: $ my_executable start # do something different... $ my_executable stop I would firs...

Invalid date when installing cucumber on Ruby 1.8.6

I'm setting up a continuous integration server for an application that uses cucumber. I'm trying to install cucumber gem on ubuntu linux 10.04 but it doesn't work on ruby 1.8.6. It works on ruby 1.8.7 but it doesn't solve my problem because cruisecontrolrb demands 1.8.6. I have found this link http://www.ruby-forum.com/topic/198581, and ...

How can I automate login on Windows Live using Ruby?

For hi5 there is a way to validate email addresses by importing your contacts from a hotmail account. I want to try and automate this task but am running into trouble. Steps to get to form page. Create hotmail email account. Using hotmail email, make a new hi5 account. After creating an account do not add photo, but navigate to home ...

Unexplainable "can't modify frozen object" exception

Hey, I have encountered a weird problem twice in the past 2 weeks and it's starting to piss me off. I have this very simple code : Rails.logger.debug "Is current_step frozen ? => #{@current_step.frozen?.inspect}" @current_step += 1 Has you can (or not) imagine, this is what is displayed on my console : Is current_step frozen ...

Can you explain what this code snippet does?

I just got finished watching a railscast episode #189 and the was using a bit_mask to store a value in the users table. I am trying to implement the same thing but when I test via irb I am getting errors. First can someone explain what this is actually going. I know what the first line is doing and part of the second line ROLES = %w[adm...

Pass block to delegated << Array method in Ruby

Hi, I have created a delegated Array class like this: class LeadPartArray < DelegateClass(Array) def <<(part, &block) super(part) unless @arr.select{|p| p.text == part.text}.size > 0 end def initialize(arr = []) @arr = arr super(@arr) end end I am overriding the << method and I want to be able to pass a block ...

What's the difference between lambda and begin block?

I am trying to verify if a text have been written to file (build.log) after executing a rake task which will throw an exception. Checkout both the code snippets below, the one with begin works whereas lambda throws a message saying it couldn't find the build.log file. Using begin to test.(works) begin Rake::Task['git:checkout'...

rails3 session store

Hi, could you tell me plz - how to use in rails3 application external Active Record session store? In rails2 its simply ActiveRecord::SessionStore::Session.establish_connection("sessions_#{RAILS_ENV}") but wat about rails3? ...

Does Ruby allow to return a slice of an array?

Perl allows you to use the '..' operator to return a slice from an array. So if I wanted cells 4 through 8 from an array, I could this: sample_array[4..8] And if I assigned that to a new array, it would only have those 5 cells. Is there an operator like this for Ruby? Thanks. ...

Defining methods on the fly in Ruby / Rails - how to set params?

I am trying to define a set of functions where I can pass in given params. for example, how do i do the following? >> get_1_type("xxx") V4_RELATIONSHIP_TYPES=[1=>2,3=>4] V4_RELATIONSHIP_TYPES.keys.each do |key| self.class.send(:define_method, "get_#{key}_type".downcase) do return GuidInfo.get_or_new(PARAMS, V4_RELATIONSHIP_TYPES[...

What's the 'Ruby way' to iterate over two arrays at once

More of a syntax curiosity than a problem to solve... I have two arrays of equal length, and want to iterate over them both at once - for example, to output both their values at a certain index. @budget = [ 100, 150, 25, 105 ] @actual = [ 120, 100, 50, 100 ] I know that I can use each_index and index into the arrays like so: @budget...

RoR set value for textarea

Hi, I cant seem to figure out how to put text in between the generate textarea tags. Is there a way to specify a value in the RoR textarea tag that will be placed between the generated textarea tags? This is an example of the code I am using. <% remote_form_for ... do |f| %> <%= f.text_area :message, %> <%= f.submit 'Upda...

Javascript Array: get 'range' of items.

Is there an equivalent for ruby's array[n..m] in Javascript ? For example: >> a = ['a','b','c','d','e','f','g'] >> a[0..2] => ['a','b','c'] Thanks ...