@Zanna Yes, it works like a bound variable in a formula. Syntactically, do |x| is kind of like a quantifier.
Like almost all programming languages, functions in Crystal (which are methods) can accept arguments. This happens when you declare parameters. For example:
def greet(name)
puts "Hello, #{name}!"
end
greet("world")
When a method has no parameters, the empty parentheses in the function definition can be omitted. I don't know what the recommended style is for that though.
There are two syntaxes for blocks: braces, and do - end.
The above code can equivalently be written as:
def thrice
yield
yield
yield
end
thrice do
puts "Hello, world!"
end
A black can accept arguments. Much as a function may have a () enclosed parameter list, which can be omitted when there are no parameters, a block may have a || enclosed parameter list, which can be omitted when there are no parameters.
def thrice(first, second, third)
yield first
yield second
yield third
end
thrice("Alice", "Bob", "Cassidy") { |name| puts "Hello, #{name}!" }
Or, with do - end:
def thrice(first, second, third)
yield first
yield second
yield third
end
thrice("Alice", "Bob", "Cassidy") do |name|
puts "Hello, #{name}!"
end
So this is what's been going on every time those syntax have appeared. In that example, which contains
server = HTTP::Server.new do |context|
context.response.content_type = "text/plain"
context.response.print "Hello world! The time is #{Time.local}"
end
that's an assignment to server, where the right-hand side is a method call to HTTP::Sserve.new, passing the block that runs from do to end. That block has one parameter, named context.
Likewise, in
icr(0.35.1) > ["Alice", "Bob", "Cassidy"].each { |name| puts name }
Alice
Bob
Cassidy
=> nil
that's a method call, each is a method on array objects, and { |name| puts name } is a block passed to it.
Likewise, to, upto, and downto are methods on numeric types like Int32, which take blocks:
@Zanna I mean involvement of persons. 3 years back, terdon, A.B. , Pilot6 , oli were more involved. They used to teach new users, and now you are doing that job.
I have moved some of the messages from the Downboat to the Island to maintain the topicality of that room. Sorry if anyone got unwanted chat invitation unintentionally. And sorry if I'm being strict :D
There are a lot of new questions on Ubuntu Freezes.
I installed Ubuntu 20.04 when it was launched, but was unable to use as my daily driver and had to move back to debian 10. There were a lot of kernel panics and freezes.
Is it just my bad, or has ubuntu became a bit unstable?
@Zanna Sorry -- I've just found the message. It did get through. I sometimes fail to notice emails. Please feel free to send follow-up messages. But also I'll try to be better about it. I've replied to your message.
@EliahKagan Since writing the messages above, I've found the Crystal documentation on blocks, and one of the things I said was inaccurate (though actually it may be inaccurate for Ruby, too). The two syntaxes for blocks are equivalent in most uses, including the examples I showed, but they have different precedence when they appear after unparenthesized method arguments.