Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden features of Ruby

People also ask

What is the features of Ruby?

Features of RubyRuby is an open-source and is freely available on the Web, but it is subject to a license. Ruby is a general-purpose, interpreted programming language. Ruby is a true object-oriented programming language. Ruby is a server-side scripting language similar to Python and PERL.

What is Ruby best known for?

Ruby is most used for building web applications. However, it is a general-purpose language similar to Python, so it has many other applications like data analysis, prototyping, and proof of concepts. Probably the most obvious implementation of Ruby is Rails web, the development framework built with Ruby.

Why is Ruby not more popular?

RoR's popularity decline is not so much because of its obsolescence, but competition. At the time of its release, this framework was one of a kind, which made it widely used in development until new products with similar or superior features began to appear.

What is the main purpose of Ruby?

Although Ruby is probably most famous for its use in web development, it has many other uses, too. Some of these include automation, command-line tools, static site generation, DevOps, web scraping, and data processing. Perhaps most importantly, Ruby is a highly versatile and portable language.


From Ruby 1.9 Proc#=== is an alias to Proc#call, which means Proc objects can be used in case statements like so:

def multiple_of(factor)
  Proc.new{|product| product.modulo(factor).zero?}
end

case number
  when multiple_of(3)
    puts "Multiple of 3"
  when multiple_of(7)
    puts "Multiple of 7"
end

Peter Cooper has a good list of Ruby tricks. Perhaps my favorite of his is allowing both single items and collections to be enumerated. (That is, treat a non-collection object as a collection containing just that object.) It looks like this:

[*items].each do |item|
  # ...
end

Don't know how hidden this is, but I've found it useful when needing to make a Hash out of a one-dimensional array:

fruit = ["apple","red","banana","yellow"]
=> ["apple", "red", "banana", "yellow"]

Hash[*fruit]    
=> {"apple"=>"red", "banana"=>"yellow"}

One trick I like is to use the splat (*) expander on objects other than Arrays. Here's an example on a regular expression match:

match, text, number = *"Something 981".match(/([A-z]*) ([0-9]*)/)

Other examples include:

a, b, c = *('A'..'Z')

Job = Struct.new(:name, :occupation)
tom = Job.new("Tom", "Developer")
name, occupation = *tom

Wow, no one mentioned the flip flop operator:

1.upto(100) do |i|
  puts i if (i == 3)..(i == 15)
end

One of the cool things about ruby is that you can call methods and run code in places other languages would frown upon, such as in method or class definitions.

For instance, to create a class that has an unknown superclass until run time, i.e. is random, you could do the following:

class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample

end

RandomSubclass.superclass # could output one of 6 different classes.

This uses the 1.9 Array#sample method (in 1.8.7-only, see Array#choice), and the example is pretty contrived but you can see the power here.

Another cool example is the ability to put default parameter values that are non fixed (like other languages often demand):

def do_something_at(something, at = Time.now)
   # ...
end

Of course the problem with the first example is that it is evaluated at definition time, not call time. So, once a superclass has been chosen, it stays that superclass for the remainder of the program.

However, in the second example, each time you call do_something_at, the at variable will be the time that the method was called (well, very very close to it)