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:32

Defining a method that accepts any number of parameters and just discards them all

def hello(*)
    super
    puts "hello!"
end

The above hello method only needs to puts "hello" on the screen and call super - but since the superclass hello defines parameters it has to as well - however since it doesn't actually need to use the parameters itself - it doesn't have to give them a name.

查看更多
看我几分像从前
3楼-- · 2019-01-04 04:33

Another fun addition in 1.9 Proc functionality is Proc#curry which allows you to turn a Proc accepting n arguments into one accepting n-1. Here it is combined with the Proc#=== tip I mentioned above:

it_is_day_of_week = lambda{ |day_of_week, date| date.wday == day_of_week }
it_is_saturday = it_is_day_of_week.curry[6]
it_is_sunday = it_is_day_of_week.curry[0]

case Time.now
when it_is_saturday
  puts "Saturday!"
when it_is_sunday
  puts "Sunday!"
else
  puts "Not the weekend"
end
查看更多
Bombasti
4楼-- · 2019-01-04 04:33

Short inject, like such:

Sum of range:

(1..10).inject(:+)
=> 55
查看更多
聊天终结者
5楼-- · 2019-01-04 04:33

use anything that responds to ===(obj) for case comparisons:

case foo
when /baz/
  do_something_with_the_string_matching_baz
when 12..15
  do_something_with_the_integer_between_12_and_15
when lambda { |x| x % 5 == 0 }
  # only works in Ruby 1.9 or if you alias Proc#call as Proc#===
  do_something_with_the_integer_that_is_a_multiple_of_5
when Bar
  do_something_with_the_instance_of_Bar
when some_object
  do_something_with_the_thing_that_matches_some_object
end

Module (and thus Class), Regexp, Date, and many other classes define an instance method :===(other), and can all be used.

Thanks to Farrel for the reminder of Proc#call being aliased as Proc#=== in Ruby 1.9.

查看更多
做自己的国王
6楼-- · 2019-01-04 04:34

module_function

Module methods that are declared as module_function will create copies of themselves as private instance methods in the class that includes the Module:

module M
  def not!
    'not!'
  end
  module_function :not!
end

class C
  include M

  def fun
    not!
  end
end

M.not!     # => 'not!
C.new.fun  # => 'not!'
C.new.not! # => NoMethodError: private method `not!' called for #<C:0x1261a00>

If you use module_function without any arguments, then any module methods that comes after the module_function statement will automatically become module_functions themselves.

module M
  module_function

  def not!
    'not!'
  end

  def yea!
    'yea!'
  end
end


class C
  include M

  def fun
    not! + ' ' + yea!
  end
end
M.not!     # => 'not!'
M.yea!     # => 'yea!'
C.new.fun  # => 'not! yea!'
查看更多
\"骚年 ilove
7楼-- · 2019-01-04 04:37

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"}
查看更多
登录 后发表回答