Subtraction, union and intersection of arrays in Ruby

Hi All.

In the exercise I’m trying to solve I have 2 arrays of names and what I
have to do in a subtraction, a union and an intersection of arrays, the
issue is that I have to use basic functions as a for loop such as this
is as indicated by the exercise, and while I understand the logic of
what to do, I can not think how to implement it correctly.

Exercise such functions done with Ruby or unused cycles would look like:

#######################################################################

op_sys = [“Fedora”, “SuSE”, “Tuquito”, “Windows”, “MacOS”]
linux_sys = [“Tuquito”, “SuSE”, “PCLinuxOS”, “Ubuntu”, “Fedora”]

sub = (op_sys) - (linux_sys)
int = (op_sys) & (linux_sys)
uni = (op_sys) | (linux_sys)

puts “Subtraction: # {rest}”
puts “intersection: # {int}”
puts “Union: # {uni}”

#########################################################################

Now if I try to do with basic functions, such as subtraction of arrays:

################################################################

op_sys = [“Fedora”, “SuSE”, “Tuquito”, “Windows”, “MacOS”]
linux_sys = [“Tuquito”, “SuSE”, “PCLinuxOS”, “Ubuntu”, “Fedora”]

for i in 0 … 4
op_sys if [i]! = linux_sys [i]
subtraction = [i] = op_sys [i]
end
end

puts “Subtraction: # {subtraction}”

#################################################################

Here is a little tricky, and not calculated well, I’ve been trying many
combinations, but do not know how to calculate the difference of arrays
using basic functions properly.

I accept any suggestions. Thank you.

Hi,

So you’re not even allowed to use basic methods like Array#include? In
this case you’ll need a nested iterator to check for every element if
it’s in the subtracted array. However, I refuse to use a “for” loop,
because it’s merely the evil twin of the “each” method and is in no way
more “basic”. Ruby isn’t C.

So the code would look something like this:

result = []
op_sys.each do |op|
add_element = true
linux_sys.each do |linux|
if op == linux
add_element = false
break
end
end
result << op if add_element
end

Your own solution I don’t understand. It seems you have some
difficulties with the programming logic and Ruby syntax.

Joao S. wrote in post #1074949:

Here is a little tricky, and not calculated well

it doesn’t even parse:

$ ruby ert.rb
ert.rb:5: syntax error, unexpected ‘!’, expecting kEND
op_sys if [i]! = linux_sys [i]
^
ert.rb:6: syntax error, unexpected ‘=’, expecting kEND
subtraction = [i] = op_sys [i]
^
ert.rb:8: syntax error, unexpected kEND, expecting $end

First make your program syntactically valid. Then you can see if it
works or not.

Note: the error message “unexpected kEND, expecting $end” is not a very
good message. What it should say is “unexpected ‘end’, was expecting
end-of-file”

That is, there is one too many ‘ends’ in your program.

Hint: a statement like

foo if bar == baz

is a single complete statement. It does not want a matching ‘end’
statement. It means “if bar == baz, do foo”

But if you write it the other way:

if bar == baz
foo
end

this means the same, but does have a matching ‘end’ statement.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs