Hidden features of Ruby

2019-01-04 04:31发布

Continuing the "Hidden features of ..." meme, let's share the lesser-known but useful features of Ruby programming language.

Try to limit this discussion with core Ruby, without any Ruby on Rails stuff.

See also:

(Please, just one hidden feature per answer.)

Thank you

30条回答
唯我独甜
2楼-- · 2019-01-04 04:50

Use a Range object as an infinite lazy list:

Inf = 1.0 / 0

(1..Inf).take(5) #=> [1, 2, 3, 4, 5]

More info here: http://banisterfiend.wordpress.com/2009/10/02/wtf-infinite-ranges-in-ruby/

查看更多
混吃等死
3楼-- · 2019-01-04 04:51

Download Ruby 1.9 source, and issue make golf, then you can do things like this:

make golf

./goruby -e 'h'
# => Hello, world!

./goruby -e 'p St'
# => StandardError

./goruby -e 'p 1.tf'
# => 1.0

./goruby19 -e 'p Fil.exp(".")'
"/home/manveru/pkgbuilds/ruby-svn/src/trunk"

Read the golf_prelude.c for more neat things hiding away.

查看更多
小情绪 Triste *
4楼-- · 2019-01-04 04:51

I find using the define_method command to dynamically generate methods to be quite interesting and not as well known. For example:

((0..9).each do |n|
    define_method "press_#{n}" do
      @number = @number.to_i * 10 + n
    end
  end

The above code uses the 'define_method' command to dynamically create the methods "press1" through "press9." Rather then typing all 10 methods which essentailly contain the same code, the define method command is used to generate these methods on the fly as needed.

查看更多
唯我独甜
5楼-- · 2019-01-04 04:51

A lot of the magic you see in Rubyland has to do with metaprogramming, which is simply writing code that writes code for you. Ruby's attr_accessor, attr_reader, and attr_writer are all simple metaprogramming, in that they create two methods in one line, following a standard pattern. Rails does a whole lot of metaprogramming with their relationship-management methods like has_one and belongs_to.

But it's pretty simple to create your own metaprogramming tricks using class_eval to execute dynamically-written code.

The following example allows a wrapper object to forwards certain methods along to an internal object:

class Wrapper
  attr_accessor :internal

  def self.forwards(*methods)
    methods.each do |method|
      define_method method do |*arguments, &block|
        internal.send method, *arguments, &block
      end
    end
  end

  forwards :to_i, :length, :split
end

w = Wrapper.new
w.internal = "12 13 14"
w.to_i        # => 12
w.length      # => 8
w.split('1')  # => ["", "2 ", "3 ", "4"]

The method Wrapper.forwards takes symbols for the names of methods and stores them in the methods array. Then, for each of those given, we use define_method to create a new method whose job it is to send the message along, including all arguments and blocks.

A great resource for metaprogramming issues is Why the Lucky Stiff's "Seeing Metaprogramming Clearly".

查看更多
爷的心禁止访问
6楼-- · 2019-01-04 04:52

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
查看更多
爱情/是我丢掉的垃圾
7楼-- · 2019-01-04 04:54

The send() method is a general-purpose method that can be used on any Class or Object in Ruby. If not overridden, send() accepts a string and calls the name of the method whose string it is passed. For example, if the user clicks the “Clr” button, the ‘press_clear’ string will be sent to the send() method and the ‘press_clear’ method will be called. The send() method allows for a fun and dynamic way to call functions in Ruby.

 %w(7 8 9 / 4 5 6 * 1 2 3 - 0 Clr = +).each do |btn|
    button btn, :width => 46, :height => 46 do
      method = case btn
        when /[0-9]/: 'press_'+btn
        when 'Clr': 'press_clear'
        when '=': 'press_equals'
        when '+': 'press_add'
        when '-': 'press_sub'
        when '*': 'press_times'
        when '/': 'press_div'
      end

      number.send(method)
      number_field.replace strong(number)
    end
  end

I talk more about this feature in Blogging Shoes: The Simple-Calc Application

查看更多
登录 后发表回答