ruby

Read a file in chunks in Ruby

I need to read a file in MB chunks, is there a cleaner way to do this in Ruby: FILENAME="d:\\tmp\\file.bin" MEGABYTE = 1024*1024 size = File.size(FILENAME) open(FILENAME, "rb") do |io| read = 0 while read < size left = (size - read) cur = left < MEGABYTE ? left : MEGABYTE data = io.read(cur) read += data.size pu...

Render different template based on where request was called from?

I'm calling a controller action to do a search with an AJAX request from 2 different pages and want to render a different rjs file based on which page requested the action. I could just make 2 actions to do this but it doesn't seem very DRY when it's the same code in the action just need different rjs as it's displaying the search result...

model class inheritance (problem with view)

Hello. I have 2 model classes with different attributes: class User < ActiveRecord::Base end class Subuser < User end When I call the 'new' function within controller: def new @subuser = Subuser.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @hosting } end end and try to access da...

hash methods argument values

Working on trying to understand the syntax for calling on different values of a hash. For example lets say I am trying to delete 'pants' How do go about setting the argument for something like this: products = {124 => ['shoes', 59.99], 352 => ['shirt', 19.99], 777 => ['pants', 19.87], 667 => ['jacket', 39.99], 898 => ['shoulder_holster'...

Call Ruby or Python API in C# .NET

I have a lot of APIs/Classes that I have developed in Ruby and Python that I would like to use in my .NET apps. Is it possible to instantiate a Ruby or Python Object in C# and call its methods? It seems that libraries like IronPython do the opposite of this. Meaning, they allow Python to utilize .NET objects, but not the reciprocal of t...

Ruby (RoR) XML to hash and then show in views

Hey. I have some xml on server (http://server.com/my.xml). Here is the example: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE current PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; <current> <song>Bruce - Inside The Machine</song> <song>02 Duel of the Fates 1</song> </current> ...

Rails Modeling Question - Relationships and Primary Keys

I'm working on a rails site that I've inherited and am trying to troubleshooting some sub-optimal model behavior. I have users, songs, and songs_download, each of which is its own model. Here's the relevant line from the users model: has_and_belongs_to_many :downloaded_songs, :class_name => 'Song', :join_table => :song_downloads F...

Should I get gems from RubyForge, Github, or Gemcutter?

I'm confused about the world of Ruby Gems. There are several well-known repositories. Which is the right one, or does it matter? I guess Gemcutter is the hip repository right now. They definitely have the nicest-looking website. Does that mean I should get my gems from there? The main reason I'm asking is that I want to make sure I'm g...

Concurrent modification whilst traversing a ruby Hash

Suppose you had this: def wipeProduct(hash, nameToDelete) hash.each do |i| key = i[0] productName = i[1].first hash.delete(key) if productName==nameToDelete end end I'm not sure it's safe to delete things from a hash whilst you're iterating over the key-value pairs of the hash. I checked the RI documentation but I ha...

How to do Ruby object serialization using JSON

I have a structure of simple container classes like so (in pseudo ruby): class A attr_reader :string_field1, :string_field2 ... end class B attr_reader: int_field3, :string_field4 ... end # C includes an instance of A and B class C attr_reader: :a_instance, :b_instance ... end Is there are simple way to de/serialize this...

Apply StyleSheet to Textile with Redcloth?

How do you apply an external stylesheet to a textile document with Ruby's Redcloth, so that I can convert it to HTML and it will apply the styles? I understand how to apply styles individually to parts of text, but how to I define a whole style (say text with font-size 20, color red, alignment x, font-family y, etc.), and apply it to al...

syntax for .max/.min and sum of hash values

Trying to get a value out of 2d array inside of a hash and determine its max or min value This is what I have so far pretend_hash = { 333 => [Dog,19.99], 222=> [Cat,25.55] } if a == 5 # Loop for view highest priced product" puts "View highest priced product" puts pretend_hash.values.max end So this returns the...

how to write a block that returns a string in ruby

A function takes a string as argument, example fn("string"). i want to pass in different values depending on three of more condiitons, and one approach which is slightly unreadable is: fn(check_1 ? "yes" : check_2 ? "no" : "maybe") i want to replace the argument with a block that returns a string. i am hoping i can do something like: ...

Manipulating local files remotely with Ruby?

I would like to automate the creation of symbolic links on my laptops from a simple Rails app running on a remote server. I would need to be able to run kernel tasks on the laptop from anywhere. Is this even possible to do? ...

Why isn't the Ruby 1.9 lambda call possible without the dot in front of the parens?

I checked out the latest Ruby version, to play a bit with the latest changes. The first thing I tried to do was call a Ruby lambda/block/proc just like you'd do with a Python callable. a = lambda {|x| puts x} a.call(4) # works, and prints 4 a[4] # works and prints 4 a.(4) # same a(4) # undefined method 'a' for main:Object Why isn't th...

How to use common named_scope for all ActiveRecord models

Hi how to build a named_scope which will be common for all models. ...

Best wiki syntax for documentation in ruby code and project README files

Hi, Are there any wiki syntax like rdoc, markdown, ... recommended in the ruby world? I write sometimes open source code and have no glue which syntax I should use in Code documents and in README files. What be helpful for me which and why you use it. ...

Assign multiple values to hash key in Ruby

For the sake of convenience I am trying to assign multiple values to a hash key in Ruby. Here's the code so far myhash = { :name => ["Tom" , "Dick" , "Harry"] } Looping through the hash gives a concatenated string of the 3 values **Output** name : TomDickHarry **Required Output** :name => "Tom" , :name => "Dick" , :name => "Harry" ...

How do you save an uploaded file to a directory and not a DB?

If someone uploads an image via a form, how do you save the image in public/images and not in a model? And I do NOT want to use plugins. ...

Bonjour in Ruby

I'm aware of dnssd, being a Ruby library for Apple's Bonjour protocol for network discovery for Mac OS X only, but is there a (relatively recent/complete) library that is cross platform? ...