ruby

How to do TOS/DSCP in ruby?

How does one set the TOS flags/DSCP flags in Ruby on a UDP/TCP stream (preferably using the Ruby/Sockets library)? ...

Ruby Object#id warnings and Active Record

We keep seeing warnings like the following when we run our specs: Object#id will be deprecated; use Object#object_id The code in question is accessing the id of an ActiveRecord model (which is an attribute on the table, obviously, rather than the object instance ID in the Ruby VM). Does anyone know how to turn these particular war...

Creating lists using yield in Ruby and Python

I'm trying to come up with an elegant way of creating a list from a function that yields values in both Python and Ruby. In Python: def foo(x): for i in range(x): if bar(i): yield i result = list(foo(100)) In Ruby: def foo(x) x.times {|i| yield i if bar(i)} end result = [] foo(100) {|x| result << x} Although I love ...

What a safe and easy way to delete a dir in Ruby?

I'd like to delete a directory that may or may not contain files or other directories. Looking in the Ruby docs I found Dir.rmdir but it won't delete non-empty dir. Is there a convenience method that let's me do this? Or do I need to write a recursive method to examine everything below the directory? ...

Stack level too deep error Ruby-Gnome2

I have a Gtk::TreeView with a Gtk::TreeModel and a Gtk::TreeModelFilter. The Tree Model is like this: category1 --> actual row of data category2 --> actual row of data I want to filter on the contents of @search_entry, but I want category1 to be displayed if a row under it is still visible, and category2 to be hidden if there are ...

Ruby Code explained

Hi, Could somebody please explain this piece of Ruby code: def add_spec_path_to(args) # :nodoc: args << {} unless Hash === args.last args.last[:spec_path] ||= caller(0)[2] end I have seen the << operator used for concatenation strings or as a bitwise operator in other languages but could somebody explain it in this context. ...

Nokogiri (RubyGem): Find and replace HTML tags

I have the following HTML: <html> <body> <h1>Foo</h1> <p>The quick brown fox.</p> <h1>Bar</h1> <p>Jumps over the lazy dog.</p> </body> </html> ...and by using the RubyGem Nokogiri (a hpricot replacement), I'd like to change it into the following HTML: <html> <body> <p class="title">Foo</p> <p>The quick brown fox.</p> <p class="title"...

In Rails - is there a rails method to convert newlines to <br>?

Is there a Railsy way to convert \n to <br>? Currently, I'm doing it like this: mystring.gsub(/\n/, '<br>') ...

Why are exclamation marks used in Ruby methods?

In Ruby some methods have a question mark (?) that ask a question like "include?" that ask if the object in question is included, this then returns a true/false. But why do some methods have exclamation marks (!) where others don't? What does it mean? ...

What's the difference between Ruby's puts and write methods?

What's the difference between... File.open('abc', 'w') { |f| f.puts 'abcde' } ...and... File.open('abc', 'w') { |f| f.write 'abcde' } ...? ...

How do I include Ruby source inline in another file?

I have a number of Ruby files, each of which declares a Class, but each of which could conceivably be run from the command line. I'd like to put the following functionality at the bottom of each file with the least duplication possible: if __FILE__ == $0 # instantiate the class and pass ARGV to instance.run end My first instinct wa...

How do you define a block of code once to be used many times?

By code block I mean: def callBlock yield yield end callBlock { puts "In the block" } #this is the block ...

Infinite yields from an iterator.

I'm trying to learn some ruby. Imagine I'm looping and doing a long running process, and in this process I want to get a spinner for as long as necessary. So I could do: a=['|','/','-','\\'] aNow=0 # ... skip setup a big loop print a[aNow] aNow += 1 aNow = 0 if aNow == a.length # ... do next step of process print "\b" But I thought i...

Generate a different range in Ruby i.e. all possible /[0-9A-Za-z]{3}/

I feel like I'm using Ruby the wrong way here: I want to generate all possible matches for the regular expression /[0-9A-Za-z]{3}/ I can't use succ because "999".succ => "1000" and "zZz".succ => "aaAa". I'm having trouble using ranges because I can't seem to union (0..9), ('A'..'Z'), ('a'..'z') So I wrote: def alphaNumeric #range an...

Common Ruby Idioms

One thing I love about ruby is that mostly it is a very readable language (which is great for self-documenting code) However, inspired by this question: http://stackoverflow.com/questions/609612/ruby-code-explained and the description of how ||= works in ruby, I was thinking about the ruby idioms I don't use, as frankly, I don't fully g...

Ideal ruby project structure

I'm after an overview/clarification of the ideal project structure for a ruby (non-rails/merb/etc) project. I'm guessing it follows along the lines of: app/ bin/ #Files for command-line execution lib/ appname.rb appname/ #Classes and so on Rakefile #Running tests README test,spe...

Map an array modifying only elements matching a certain condition

In Ruby, what is the most expressive way to map an array in such a way that certain elements are modified and the others left untouched? This is a straight-forward way to do it: old_a = ["a", "b", "c"] # ["a", "b", "c"] new_a = old_a.map { |x| (x=="b" ? x+"!" : x) } # ["a", "b!", "c"] Omitting the "leave-alon...

How to change html tag attribute value from RJS template?

Is it possible to change a html tag attribute value from an RSJ template? I know that there is a page.replace_html method, but it is not very useful in my case, since I have lengthy values of various attributes (such as alt, title of an image). What I want is change src attribute of a img tag in RJS. Is that possible at all? Thank you. ...

Best way to iterate over a sequence and change one object at a time (Ruby)

I'm new to ruby and feel that I still do a lot of things in C sharpish way :-). Suppose you have an array of objects (Question :has_many=>:answers). I want to iterate over all answers and if some answers meet a criteria, change answer attribute. Currently I'm doing it as follows: def iterate_and_do_stuff for (a in self.answers) ...

Integrating models and views in new Sinatra extensions

So I would like to use the new possibility to create extensions for Sinatra. My Extension needs to integrate a model and some views/templates but I don't know how to or where to integrate them? Did anybody already built something more complex than the example from the documentation? ...