What Is "For" in Ruby

What is for in Ruby

It's almost syntax sugar. One difference is that, while for would use the scope of the code around it, each creates a separate scope within its block. Compare the following:

for i in (1..3)
x = i
end
p x # => 3

versus

(1..3).each do |i|
x = i
end
p x # => undefined local variable or method `x' for main:Object

What is for in Ruby

It's almost syntax sugar. One difference is that, while for would use the scope of the code around it, each creates a separate scope within its block. Compare the following:

for i in (1..3)
x = i
end
p x # => 3

versus

(1..3).each do |i|
x = i
end
p x # => undefined local variable or method `x' for main:Object

What is the method symbol for += in ruby?

It is not a method. It is a short way (syntactic sugar) for writing following:

x = 1
#=> 1
x += 1 # same as x = x + 1
#=> 2

what does @ stand for in a Ruby function name

The method names for the four unary operators +, -, ~, and ! are +@, -@, ~@, and !@. So the funny looking method definitions:

def +@; _show _test :_pass, :_fail end
def -@; _show _test :_fail, :_pass end
def ~@; _show _pend; end
def !@; _show _desc; end

just define overloads for those four unary operators. Then TestRocket is patched into the Proc class using Proc.send :include, TestRocket.

This:

-> { Die.new(2) }

is simply a lambda definition and another way of writing lambda { Die.new(2) }. Then, with TestRocket patched into Proc we can say this:

+-> { Die.new(2) }
# + lambda { Die.new(2) }

and it will run this method:

def +@; _show _test :_pass, :_fail end

as an instance method on that lambda.

Looks like a bit of an abuse of the unary operator overloading to "invent" something that looks like new -->, ~->, ... operators.

What does ||= (or-equals) mean in Ruby?

This question has been discussed so often on the Ruby mailing-lists and Ruby blogs that there are now even threads on the Ruby mailing-list whose only purpose is to collect links to all the other threads on the Ruby mailing-list that discuss this issue.

Here's one: The definitive list of ||= (OR Equal) threads and pages

If you really want to know what is going on, take a look at Section 11.4.2.3 "Abbreviated assignments" of the Ruby Language Draft Specification.

As a first approximation,

a ||= b

is equivalent to

a || a = b

and not equivalent to

a = a || b

However, that is only a first approximation, especially if a is undefined. The semantics also differ depending on whether it is a simple variable assignment, a method assignment or an indexing assignment:

a    ||= b
a.c ||= b
a[c] ||= b

are all treated differently.

Why is `for i in a` not idiomatic Ruby?

For me, it's uniform syntax. When I am going to do something with array a, I just start typing a.. What am I going to do? Filter? a.select; determine the size? a.size. Iterate? a.each etc.

Iterating an array is nothing special and requires no special language construct. Also eachwithout an immediate block results in an Enumerator, which can be chained with other methods.

Then again, Ruby does have a fine for i in a loop. So if you are more comfortable with a for loop: by all means, use a for loop.

What is Ruby's double-colon `::`?

:: is basically a namespace resolution operator. It allows you to access items in modules, or class-level items in classes. For example, say you had this setup:

module SomeModule
module InnerModule
class MyClass
CONSTANT = 4
end
end
end

You could access CONSTANT from outside the module as SomeModule::InnerModule::MyClass::CONSTANT.

It doesn't affect instance methods defined on a class, since you access those with a different syntax (the dot .).

Relevant note: If you want to go back to the top-level namespace, do this: ::SomeModule – Benjamin Oakes

For loops while i 10 in Ruby

The "ruby-ish" way would be each

(1...10).each do |i|
puts i
end

or to double iterate

(1...10).each do 
(1...10).each do |j|
puts j
end
end

As @maxwilliams and @sawa point out, (1...10) is 1 through 9 inclusive, (1..10) is 1 through 10 inclusive.

What is stand for in ruby with integer

As the documentation says, Integer:<< - Returns the integer shifted left "X" positions, or right if "X" is negative. In your scenario is shifts 8 positions to the left.

Here is how it works:

8.to(2) => "1000"

Now let's shift "1000" 8 positions to the left

(8 << 8).to_s(2) => "100000000000"

If you count the 0 above you will see it added 8 after "1000".

Now, let's see how it returns 2048

"100000000000".to_i(2) => 2048


Related Topics



Leave a reply



Submit