Ways to express proc inside a method I'm interested in seeing different ways the proc can be expressed inside a method that takes args.

This method is meant to be a sort of FizzBuzz that will dynamically replace "Fizz" or "Buzz" as well as the traditional divisors 3 &amp; 5.

def fizz_buzz_custom(string_1="Fizz", string_2="Buzz", num_1=3, num_2=5)
  proc do |length|
    index = length + 1 
    val = if (index % 3 == 0 &amp;&amp; index &amp; 5 == 0)
      (string_1+string_2)
    elsif index % 3 == 0
      string_1
    elsif index % 5 == 0
      string_2      
    else
      index
    end
  end
end


Here are some examples with test output:

fizz_buzz_custom[15]                         # returns 16
fizz_buzz_custom[44]                         # returns "FizzBuzz" (45 is divisible by 3 and 5)
fizz_buzz_custom('Hey', 'There')[25]         # returns 26
fizz_buzz_custom('Hey', 'There')[11]         # returns "Hey" (12 is divisible by 3)
fizz_buzz_custom("What's ", "up?", 3, 7)[80] # returns "What's " (81 is divisible by 3)
To answer your question directly, you can express a proc/block/lambda as

Proc.new do |...|
# or
lambda do |...|
# or
-&gt; (...) do


Either syntax (and your current one) should work fine for this. To invoke the block, you can use square brackets like now, or use call.

Really, that's about it, unless I'm misinterpreting your question.

However, I see some other issues with your code:


Use a hash for the arguments. Please. The current construction is very un-Ruby-like. A hash will also "force" you to write the method (or block, in this case) in a more generic manner, since a hash can have any number of key/value pairs.
Why length? It's just a number. Whose to say it's the length of anything?
Also, why add 1 to length? Just wondering. If anything, going from an array's length to an index usually involves subtracting 1.


Something like this seems cleaner (though it's as much about FizzBuzz as it is about procs):

def custom_fizz_buzz(divisors = {"Fizz" =&gt; 3, "Buzz" =&gt; 5})
  divisors = divisors.sort_by { |_, value| value }
  -&gt; (number) do
    output = divisors.map { |key, value| key if (number % value).zero? }.compact.join
    output.empty? ? number : output
  end
end


which'll give you

custom_fizz_buzz[7]                              # =&gt; 7
custom_fizz_buzz.call(9)                         # =&gt; "Fizz"
custom_fizz_buzz[15]                             # =&gt; "FizzBuzz"
custom_fizz_buzz("What's " =&gt; 3, "up?" =&gt; 7)[21] # =&gt; "What's up?"


or even

1.upto(100).map(&amp;custom_fizz_buzz) # =&gt; [1, 2, "Fizz", 4, "Buzz", .... etc.]