ruby


How to understand symbols in Ruby

Consider this: x = :sym y = :sym (x.__id__ == y.__id__ ) && ( :sym.__id__ == x.__id__) # => true x = "string" y = "string" (x.__id__ == y.__id__ ) || ( "string".__id__ == x.__id__) # => false So, ho... Read More


class << self vs self.method with Ruby: what's better?

class << self is good at keeping all of your class methods in the same block. If methods are being added in def self.method form then there's no guarantee (other than convention and wishful thinking)... Read More


ruby - What's the difference between request.remote_ip and request.ip in Rails?

request.ip returns the client ip even if that client is a proxy. request.remote_ip is smarter and gets the actual client ip. This can only be done if the all the proxies along the way set the X-Forwa... Read More


ruby - How do I return early from a rake task?

A Rake task is basically a block. A block, except lambdas, doesn't support return but you can skip to the next statement using next which in a rake task has the same effect of using return in a metho... Read More


ruby - Array include any value from another array?

(cheeses & foods).empty? As Marc-André Lafortune said in comments, & works in linear time while any? + include? will be quadratic. For larger sets of data, linear time will be faster. For small data... Read More


shell - Continuously read from STDOUT of external process in Ruby

I've had some success in solving this problem of mine. Here are the details, with some explanations, in case anyone having a similar problem finds this page. But if you don't care for details, here's... Read More


ruby - rails console doesn't load due to libreadline

Ran across this today, to solve it I did: brew rm -f readline brew install readline brew link readline --force Hope it helps. EDIT: I recently ran into this problem again (after downgrading Ruby) s... Read More


Ruby syntax: break out from 'each.. do..' block

You can break with the break keyword. For example [1,2,3].each do |i| puts i break end will output 1. Or if you want to directly return the value, use return. Since you updated the question, here... Read More


ruby - How to get activerecord associations via reflection

Model.reflections gives information about a model's associations. It is a Hash keyed on the association name. e.g. Post.reflections.keys # => ["comments"] Here is an example of some of the informatio... Read More


How do I parse a YAML file in Ruby?

Maybe I'm missing something, but why try to parse the file? Why not just load the YAML and examine the object(s) that result? If your sample YAML is in some.yml, then this: require 'yaml' thing = YAM... Read More