Questions tagged [block]

DO NOT USE This tag is scheduled for removal because it can refer to many different things depending on the use of other tags with it.

This tag has an active burnination. Remove it from questions if a) you can improve the question in other ways at the same time or b) you have 2k+ rep.

Depending on the tags it's used with, it can refer to:

  • Blocking access to certain resources.
  • Blocks specific to the program or language its used within.
  • Blocks in a file system.
3655 questions
42
votes
2 answers

Referring to weak self inside a nested block

Suppose I already create a weak self using __weak typeof(self) weakSelf = self; [self doABlockOperation:^{ ... }]; Inside that block, if I nest another block: [weakSelf doAnotherBlockOperation:^{ [weakSelf doSomething]; } will it…
Enzo Tran
  • 5,750
  • 6
  • 31
  • 36
40
votes
7 answers

Best explanation of Ruby blocks?

What is the best explanation for Ruby blocks that you can share? Both usage and writing code that can take a block?
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
39
votes
3 answers

Can someone explain Ruby's use of pipe characters in a block?

Can someone explain to me Ruby's use of pipe characters in a block? I understand that it contains a variable name that will be assigned the data as it iterates. But what is this called? Can there be more than one variable inside the pipes? Anything…
Josh Curren
  • 10,171
  • 17
  • 62
  • 73
38
votes
1 answer

Are there better ways to prevent 'yield' when no block is passed in?

I have a method that yields, which looks like: def a_method(*params) # do something yield # do something else end I want this method to yield the block if a block is passed in; and if no block is passed in, the method should sliently skip the…
Tao
  • 970
  • 1
  • 12
  • 21
37
votes
1 answer

How can I determine if a Jinja2 template block is empty?

I'd like to detect if a Jinja2 template block content is empty or not. Something like this: {% block foo %}{% endblock foo %}{% if foo %} - {% endif %}Blah Blah Blah What I want is conditional text outside the block definition itself. In the…
Inactivist
  • 9,997
  • 6
  • 29
  • 41
37
votes
2 answers

Make big quotes with

Some years ago, I used the tag to create a quote on my site (with big quotation marks). Now I want to do the same thing, but it doesn't work anymore. The only thing I get are small "" and not the big ones. How do I get the old, big ones…
Matt
  • 1,893
  • 11
  • 33
  • 57
36
votes
5 answers

How to stop enumerateObjectsUsingBlock Swift

How do I stop a block enumeration? myArray.enumerateObjectsUsingBlock( { object, index, stop in //how do I stop the enumeration in here?? }) I know in obj-c you do this: [myArray enumerateObjectsUsingBlock:^(id *myObject,…
random
  • 8,568
  • 12
  • 50
  • 85
34
votes
1 answer

How to get block cyclic distribution?

I am trying to distribute my matrix in block cyclic fashion. I learned a lot from this question (MPI IO Reading and Writing Block Cyclic Matrix), but that is not what I really need. Let me explain my problem. Suppose I have this matrix of dimension…
matiska
  • 525
  • 6
  • 14
33
votes
2 answers

How to end execution of a block in Ruby?

I thought blocks were like anonymous functions. But when I tried to end the execution of a block using return keyword, I think it triggered a return in the scope in which the block was defined in. Is that how they work? If so, how can I end the…
HappyDeveloper
  • 12,480
  • 22
  • 82
  • 117
31
votes
5 answers

Loop & output content_tags within content_tag in helper

I'm trying a helper method that will output a list of items, to be called like so: foo_list( ['item_one', link_to( 'item_two', '#' ) ... ] ) I have written the helper like so after reading Using helpers in rails 3 to output html: def foo_list…
DEfusion
  • 5,533
  • 5
  • 44
  • 60
31
votes
3 answers

How do I run multiple lines of Ruby in html.erb file

I'm using Ruby on Rails and need to run a block of Ruby code in one of my html.erb files. Do I do it like this: <% def name %> <% name = username %> <%= name %> or like this: <% def name name = username %> <%= name %> Thanks for reading.
ben
  • 29,229
  • 42
  • 124
  • 179
28
votes
4 answers

Can I reference a lambda from within itself using Ruby?

I want to be able to call an anonymous lambda from within itself using Ruby. Consider the following recursive block (returns a factorial). I know I can assign it to a variable, and that variable is within the scope of the lambda: fac = lambda { |n|…
Edd Morgan
  • 2,873
  • 17
  • 22
28
votes
4 answers

Why do instance variables seemingly disappear when inside a block?

Forgive me, guys. I am at best a novice when it comes to Ruby. I'm just curious to know the explanation for what seems like pretty odd behavior to me. I'm using the Savon library to interact with a SOAP service in my Ruby app. What I noticed is that…
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
27
votes
4 answers

Difference Between Completion Handler and Blocks : [iOS]

I am messed with both completion handler and blocks while I am using them in Swift and Objective-C. And when I am searching blocks in Swift on google it is showing result for completion handler! Can somebody tell me what is the difference between…
shubham mishra
  • 971
  • 1
  • 13
  • 32
27
votes
1 answer

How to store ruby code blocks

I want to store a "code block" in a variable to be reused, something like: block = do |test| puts test end 3.upto(8) block Can someone show me what am I doing so obviously wrong? (Or if it's just impossible)
user1241335