Running methods on Iterations

Hello,

I’m new to ruby and I’m going through the RubyMonk tutorials. I’m on the
problem 3.2 Iteration looping.

The question is use the each method to store values in source that is
less
than 4 into destination.

def array_copy(source)

destination = []

your code

return destination
end

The answer was

source.each do | i |

destination << i if i < 4
end

My first question is why are we iterating over source not array_copy
which
is the method containing all the source? Secondly, why don’t we have to
close the ‘if’ statement?

Thank you for your help!

The simplest way to extract the matching values would be to use
Array#select, which retains all the elements where the block evaluates
truthfully.

def array_copy(source)
source.select { |val| val < 4 }
end

Ruby’s syntax allows for some single-line conditionals, including these
constructs:

method if condition

method unless condition

If you wanted to have an else in a single-line condition, the simplest
way would be to use the ternary operator:

condition ? true_method : false_method

John M. wrote in post #1124612:

My first question is why are we iterating over source not array_copy
which
is the method containing all the source?

I forgot to address this question.

The destination Array at first is an empty Array. Iterating over it
would result in 0 iterations.

On Tue, Oct 15, 2013 at 6:25 PM, Joel P. [email protected]
wrote:

The simplest way to extract the matching values would be to use
Array#select, which retains all the elements where the block evaluates
truthfully.

def array_copy(source)
source.select { |val| val < 4 }
end

Right, but I guess that task was about something else. :slight_smile:

Ruby’s syntax allows for some single-line conditionals, including these
constructs:

method if condition

method unless condition

Actually it’s “expression” instead of “method”. You can even do

irb(main):001:0> 1 if 2 > 3
=> nil
irb(main):002:0> 1 if 2 < 3
=> 1

If you wanted to have an else in a single-line condition, the simplest
way would be to use the ternary operator:

condition ? true_method : false_method

Maybe it’s worthwhile to mention that if then else is actually an
expression (actually everything is in Ruby) and not a statement as in
some other programming languages. You can even fit it “nicely” on a
single line:

irb(main):009:0> if 1 > 0; ‘a’ else ‘b’ end
=> “a”
irb(main):010:0> if 1 < 0; ‘a’ else ‘b’ end
=> “b”

But I agree: the ternary operator is much more elegant.

On Tue, Oct 15, 2013 at 6:26 PM, Joel P. [email protected]
wrote:

John M. wrote in post #1124612:

My first question is why are we iterating over source not array_copy
which is the method containing all the source?

The destination Array at first is an empty Array. Iterating over it
would result in 0 iterations.

Since John asked about iterating “array_copy” I’d like to add:
array_copy is actually the name of the method - it’s not an Array or
other collection of values like “source” is. So you cannot iterate
“array_copy”. And as Joel explained to copy something from a to b you
need to go through a - and nothing else. :slight_smile:

Kind regards

robert

Thanks so much for the answers!

This is helping me get a better understanding of calling a method on an
object. It is interesting to see how in some cases the if needs to be
closed and in some it can simply run as an expression

Anyways. Thanks again!
John

On Tue, Oct 15, 2013 at 12:48 PM, Robert K.

FYI, Ruby also supports this syntax
if 1==1 then 1 else 2 end

On Tue, Oct 15, 2013 at 7:48 PM, John M. [email protected] wrote:

Thanks so much for the answers!

You’re welcome.

This is helping me get a better understanding of calling a method on an
object. It is interesting to see how in some cases the if needs to be closed
and in some it can simply run as an expression

Conditional expressions with “if” (and all the others) are always
expressions - there are no statements in Ruby. You can see that they
always evaluate to something:

irb(main):003:0> 1 if 2
=> 1
irb(main):004:0> 1 if false
=> nil
irb(main):005:0> if 2 then 1 end
=> 1
irb(main):006:0> if false then 1 end
=> nil

Even loop expressions evaluate to something:

irb(main):009:0> i=0
=> 0
irb(main):010:0> while i < 3; i += 1 end
=> nil
irb(main):011:0> i = 0
=> 0
irb(main):012:0> i += 1 while i < 3
=> nil

Kind regards

robert