When is the do keyword required in Ruby?
According to The Ruby Programming Language book Section 5.2.1:
The
do
keyword in awhile
oruntil
loop is like thethen
keyword in anif
statement: it may be omitted altogether as long as a newline (or
semicolon) appears between the loop condition and the loop body.
So, no, it won't change the behavior, it's just optional syntax.
What is the meaning of the keyword do, in ruby?
do ... end
(or alternatively { ... }
) creates a so-called block, which is a type of anonymous function in Ruby. In your example that block is passed as an argument to group
. group
then does some bookkeeping to set the given groups as active, executes the block, and then deactivates the groups again.
What does this kind of keyword use do in Ruby?
layout
isn't a keyword. layout false
is the same as layout(false)
. The parentheses are optional for method calls in Ruby.
See the layout
documentation for details of the layout()
method in Rails.
What's the purpose of `do` in the while loop?
The non-theoretical answer is quite simply: because the Ruby Syntax Guide said so.
The language syntax guide defines the do
keyword as optional for both while
and until
loops.
From what I understand - and it is mostly a theory - allowing the do
to be optional rather than required is mostly for compatibility and allowing for harmonic syntax within the language.
I think of it as an acknowledgment for the "gray" areas, where things are less absolute. The same is done in physics, where light behaves both as a particle and as a wave and we should acknowledge both aspects.
Coming from different languages and school of thought, while
is somewhere between a pure keyword (like if
) and a method (like loop
, which is defined under the Kernel module)
if
statements do not require a do
to begin a block of code and under the same logic, while
loops wouldn't require a do
keyword.
This is while
as a keyword.
On the other hand, loop
requires the do
keyword (or the {block}
), so why shouldn't while
have the same semantics?
This is while
as a method (as far as syntax goes).
Ruby is about making the programmer happy. Allowing the do
to be optional makes all programmers happy and doesn't require that the Ruby programmer resign themselves to just one school of thought related to the nature of while
.
Where are keywords defined in Ruby?
The keywords are not objects but defined in the parser which can be found in parse.y
in the Ruby source. Here's the relevant part from that file:
reswords : keyword__LINE__ | keyword__FILE__ | keyword__ENCODING__
| keyword_BEGIN | keyword_END
| keyword_alias | keyword_and | keyword_begin
| keyword_break | keyword_case | keyword_class | keyword_def
| keyword_defined | keyword_do | keyword_else | keyword_elsif
| keyword_end | keyword_ensure | keyword_false
| keyword_for | keyword_in | keyword_module | keyword_next
| keyword_nil | keyword_not | keyword_or | keyword_redo
| keyword_rescue | keyword_retry | keyword_return | keyword_self
| keyword_super | keyword_then | keyword_true | keyword_undef
| keyword_when | keyword_yield | keyword_if | keyword_unless
| keyword_while | keyword_until
;
If you want to know more about the Ruby parser, look at the presentation Hacking parse.y from RubyConf 2009 or Parse.y famtour from Ruby Kaigi 2011.
Also, a lot of the methods that are available everywhere (like e.g. puts
) are defined in the Kernel module.
EDIT: There's also a list of key words in the documentation, thanks @antinome for pointing that out.
What is the on keyword in ruby?
It's not a keyword, but a simple instance method called on the Cinch::Bot
object you just created. The block you're passing to Cinch::Bot.new
is evaluated against that new object (https://github.com/cinchrb/cinch/blob/master/lib/cinch/bot.rb#L363). It's a neat trick called a "DSL" (Domain-Specific Language): it looks like magic keywords or global methods, but it's really just methods called on a normal object.
Looks like on
is defined in https://github.com/cinchrb/cinch/blob/master/lib/cinch/bot.rb#L188.
The code could instead be written like this:
bot = Cinch::Bot.new
bot.on :channel do |m|
...
end
ruby: What's the meaning of keyword in
You should be able to do the following:
for i in 0..10 do
puts i
end
The expression 1 in (0..10)
that you mention won't work because a constant (1) can't vary over a range - it's a constant! You need to name a variable before the in
keyword.
Hope that helps.
See this page as well.
Related Topics
Ruby Parenthesis Syntax Exception with I++ ++I
Splitting a String into Words and Punctuation with Ruby
How to Build a Docker Image for a Ruby Project Without Build Tools
Open Xml File with Nokogiri Update Node and Save
Getting Error (Exit Code 2) for Bootsnap, Not Allowing to Set Up Rails Server
P Method in Ruby Hard to Search For
Why Should You Avoid the Then Keyword in Ruby
System New Line Separator in Ruby
Execute Code Once Sinatra Server Is Running
Why Is the Timezone Off in Delayed_Job
Incompatible Character Encoding in Rails - How to Just Fail/Skip Sensibly
Map Array of Ints to Nested Array Access
Mechanize and Ntlm Authentication
How to Access HTML Elements That Are Rendered in JavaScript Using Xpath
How to Dry Up Method with Multiple { 'Not Found' }
Gmaps4Rails:Setting Map Width and Height
Dangerousattributeerror in Omniauth Railscast Tutorial: Create Is Defined by Activerecord