Questions tagged [ruby-block]
37 questions
                    
                    309
                    
            votes
                
                10 answers
            
        Blocks and yields in Ruby
I am trying to understand blocks and yield and how they work in Ruby.
How is yield used? Many of the Rails applications I've looked at use yield in a weird way.
Can someone explain to me or show me where to go to understand them?
         
    
    
        Matt Elhotiby
        
- 43,028
- 85
- 218
- 321
                    138
                    
            votes
                
                5 answers
            
        Rails render partial with block
I'm trying to re-use an html component that i've written that provides panel styling.  Something like:
  
                    
                    43
                    
            votes
                
                1 answer
            
        Ruby: Use the return of the conditional for variable assignment and comparison
I have a method and in order to check whether it is being passed a block, I do the following:
if block_given?
    res = yield(array[i], array[i+1])
  else
    res = array[i] - array[i+1]
  end
However RuboCop is giving me a warning which I don't…
         
    
    
        noloman
        
- 11,411
- 20
- 82
- 129
                    24
                    
            votes
                
                4 answers
            
        Why does Array#each return an array with the same elements?
I'm learning the details of how each works in ruby, and I tried out the following line of code:
p [1,2,3,4,5].each { |element| el }
And the result is an array of
[1,2,3,4,5]
Why is the return value of each the same array? Doesn't each just provide…
         
    
    
        Jeff Storey
        
- 56,312
- 72
- 233
- 406
                    7
                    
            votes
                
                1 answer
            
        How to chain a method call to a `do ... end` block in Ruby?
I'm doing the following:
array_variable = collection.map do |param|
  some value with param
end
return array_variable.compact
Can I call map and compact in one statement somehow, so I can return the result instantly?
I'm thinking on something like…
         
    
    
        MattSom
        
- 2,097
- 5
- 31
- 53
                    7
                    
            votes
                
                3 answers
            
        Ruby - What are the differences between checking if block_given? and !block.nil?
I have a ruby method that needs to check if a block was passed to it. 
A colleague is suggesting that simply checking if block.nil? is slightly faster in performance and works for named blocks. This is already quite annoying since he is using the…
         
    
    
        singletony
        
- 1,392
- 1
- 9
- 9
                    6
                    
            votes
                
                2 answers
            
        How to check block is called using rspec
I want to check whether the block is called in my function using rspec. Below is my code:
class SP
  def speak(options={},&block)
    puts "speak called" 
    block.call()
    rescue ZeroDivisionError => e
  end  
end
describe SP do
 it "testing…
         
    
    
        ojas
        
- 2,150
- 5
- 22
- 37
                    3
                    
            votes
                
                2 answers
            
        Pass a block to map function
I was wondering if something like this was possible?
info = arrange_info({|x| [x.name, x.number]}, info_array)
def arrange_info(block, info)
    info.map(block).to_h
end
This would allow me to pass different blocks to arrange the array is…
         
    
    
        user2320239
        
- 1,021
- 2
- 18
- 43
                    2
                    
            votes
                
                1 answer
            
        Ruby block as an array element for splatting in public_send
I am trying to build a generic method using meta programming where it uses manipulating methods from array and send to the object using splat, following is the working snippet:
ALLOWED_DATA_TYPES = {
  'Integer' => [['to_i']],
  'Csv' => [['split',…
         
    
    
        Md. Farhan Memon
        
- 6,055
- 2
- 11
- 36
                    2
                    
            votes
                
                2 answers
            
        Does `block_given?` render `&block` parameter optional?
I am comfortable with the following:
def some_def(foo, &block)
    puts "block utilized below"
    block.call(foo)
end
def some_other_def(bar)
    puts "using yield below"
    yield bar
    puts "and back into the method"
end
So I have learned to…
         
    
    
        foo
        
- 195
- 1
- 8
                    2
                    
            votes
                
                4 answers
            
        How can I chain with_index and with_object on an enumerable in ruby?
I want to process the array ['a', 'b', 'c'] to return the string '0a1b2c' (i.e, string formed by concatenating each index with its value).
I can do this:
result = ''
['a', 'b', 'c'].each.with_index do |char, i|
  result += "#{i}#{char}"
end …
         
    
    
        Anand
        
- 3,690
- 4
- 33
- 64
                    1
                    
            vote
                
                1 answer
            
        Pass a method that receives args to another method that takes a block
I have some code that needs to be called directly or passed to another method that must take a block. Pseudocode:
class Foo
  def bar
    if condition
      return method_that_needs_a_block!('string', named1:, named2:) do
        shared_method('a',…
         
    
    
        ryanpback
        
- 275
- 4
- 17
                    1
                    
            vote
                
                1 answer
            
        Optional explicit block parameter in Ruby?
As noted in the comments, the question is actually the answer.
If the method gets a block, I want to use it further. But I also have a variant where the block isn’t needed. Can I do this in any way?
For example:
def maybe_gets_block(&blk)
  if blk
 …
        user13775669
                    1
                    
            vote
                
                0 answers
            
        Unable to set the node attributes within a ruby block
I have exactly the same code as suggested in the given link by @StephenKing (https://stackoverflow.com/users/400222/stephenking) and although the command works and gives the correct return value (verified using log/print) the value is not set in the…
         
    
    
        blueowl
        
- 35
- 6
                    1
                    
            vote
                
                1 answer
            
        Get the name of the currently if condition declaration
I was wondering if it was possible to pass as parameter the if condition statement as string or symbol. Because method name or if statement name could change and if I need it to refacto things it's better to work with a variable, 
here an example…
         
    
    
        v2lrf
        
- 321
- 3
- 8
