« first day (1048 days earlier)      last day (1351 days later) » 

5:07 AM
@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")
 
nice
that's at least slightly familiar
 
Similarly you can declare multiple parameters, which is the main way to accept multiple arguments:
def show_rectangle(height, width)
  height.times { puts "*" * width }
end

show_rectangle(4, 12)
 
@EliahKagan neat! ^_^
 
5:28 AM
Or zero parameters:
# Reads a line as an array of integers.
def read_record()
  if line = gets
    line.split.map(&.to_i)
  else
    [] of Int32
  end
end

pp read_record
@Zanna I mean, you might prefer to show a rectangle in a GUI window or a web page. :)
 
I made one out of @ symbols instead of *
 
:)
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.
 
do you mean whether one should omit them or not?
 
The reason I bring all of this up is that methods can also accept blocks.
 
there was a mention of blocks on that HTTP server example page, but I did not work out exactly what part of the code it was referring to
 
5:35 AM
A method that uses the yield keyword accepts a block. yield calls the block.
def thrice
  yield
  yield
  yield
end

thrice { puts "Hello, world!" }
 
hahaha what a nice example
 
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:
icr(0.35.1) > 1.to(5) { |width| puts "#" * width }
#
##
###
####
#####
 => nil
Those methods take both an argument, supplying the second endpoint (in that example, 5), and a block.
My original thrice method is not needed, since integers have a times method, which takes a block and yields to it that number of times:
icr(0.35.1) > 3.times { puts "Hello, world!" }
Hello, world!
Hello, world!
Hello, world!
 => nil
 
 
2 hours later…
7:23 AM
@Zanna Hye Mod.. are you really from chennai?
 
@kashish I live in Chennai, but I'm from UK
 
oh cool. are you doing job there?
 
@Zanna UK is UttaraKhand for Indians :P
 
@kashish no, I only have work in the UK which I do remotely over the internet (although this year I don't really have work because of corona
@Kulfy lol thanks for that
@kashish I am from England, UK, i.e. I am a foreigner
 
@Zanna cool, cool. yes I get that (UK).. Visited AU after a long time. things changed a lot here.
 
7:31 AM
oh yes? I joined in 2016. Have seen some changes since then, but not sure what you are noticing
 
@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.
 
7:50 AM
11 messages moved from Raiders of the Lost Downboat
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
 
8:21 AM
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?
 
 
1 hour later…
9:33 AM
1 message moved from Raiders of the Lost Downboat
 
 
7 hours later…
4:49 PM
@EliahKagan how helpful
idk how you manage to work out all these things so nicely in a language you only just met
 
5:02 PM
@Zanna Well, a lot of this is very similar to Ruby.
 
BTW I did try to email you last time you were awayish but I guess I did something wrong
 
@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.
 
I fail to notice a lot of emails
@EliahKagan haha this is just like what you were doing
the example there, I mean
 
5:21 PM
Yeah. :)
I mean, that documentation is higher quality.
 
5:46 PM
I was thinking you could be writing a textbook about it
but sometimes textbooks are awful so that kind of fails as a compliment
I think I've been processing my relief over that meta situation all day. I certainly haven't got anything else done
Have to call it a day
sorry
 
6:04 PM
No problem!!
 

« first day (1048 days earlier)      last day (1351 days later) »