What is Ruby’s double-colon `::`?

:: is basically a namespace resolution operator. It allows you to access items in modules, or class-level items in classes. For example, say you had this setup: module SomeModule module InnerModule class MyClass CONSTANT = 4 end end end You could access CONSTANT from outside the module as SomeModule::InnerModule::MyClass::CONSTANT. It doesn’t affect instance methods defined … Read more

Getting output of system() calls in Ruby

I’d like to expand & clarify chaos’s answer a bit. If you surround your command with backticks, then you don’t need to (explicitly) call system() at all. The backticks execute the command and return the output as a string. You can then assign the value to a variable like so: output = `ls` p output … Read more

Does ruby have real multithreading?

Updated with Jörg’s Sept 2011 comment You seem to be confusing two very different things here: the Ruby Programming Language and the specific threading model of one specific implementation of the Ruby Programming Language. There are currently around 11 different implementations of the Ruby Programming Language, with very different and unique threading models. (Unfortunately, only … Read more

Installed Ruby 1.9.3 with RVM but command line doesn’t show ruby -v

You have broken version of RVM. Ubuntu does something to RVM that produces lots of errors, the only safe way of fixing for now is to: sudo apt-get –purge remove ruby-rvm sudo rm -rf /usr/share/ruby-rvm /etc/rvmrc /etc/profile.d/rvm.sh open new terminal and validate environment is clean from old RVM settings (should be no output): env | … Read more

How to pass command line arguments to a rake task

You can specify formal arguments in rake by adding symbol arguments to the task call. For example: require ‘rake’ task :my_task, [:arg1, :arg2] do |t, args| puts “Args were: #{args} of class #{args.class}” puts “arg1 was: ‘#{args[:arg1]}’ of class #{args[:arg1].class}” puts “arg2 was: ‘#{args[:arg2]}’ of class #{args[:arg2].class}” end task :invoke_my_task do Rake.application.invoke_task(“my_task[1, 2]”) end # … Read more

How to call methods dynamically based on their name? [duplicate]

What you want to do is called dynamic dispatch. It’s very easy in Ruby, just use public_send: method_name=”foobar” obj.public_send(method_name) if obj.respond_to? method_name If the method is private/protected, use send instead, but prefer public_send. This is a potential security risk if the value of method_name comes from the user. To prevent vulnerabilities, you should validate which … Read more