Unexpected Ruby behavior when instantiating two instances on one line - ruby

I created a class that generates a different name for each instance, but a test fails unexpectedly when instantiating two instances in one statement.
Here is the class.
class Robot
attr_accessor :name
##current_name = 'AA000'
def initialize
#name = ##current_name
##current_name.next!
end
end
Here the class behaves as expected
irb(main):009:0> Robot.new.name
=> "AA001"
irb(main):010:0> Robot.new.name
=> "AA002"
Here is the unexpected behavior, I was expecting false. This code is in a test on an exercise I am trying to get passing, so I can't change the test.
irb(main):011:0> Robot.new.name == Robot.new.name
=> true
Checking the object_id reveals that two different instances are being created.
irb(main):012:0> Robot.new.object_id == Robot.new.object_id
=> false
Why is Ruby doing this, what should I do to fix it & assuming there is a term for this, what could I have typed into search to find answered questions about this.

See if this helps:
class Robot
attr_accessor :name
##current_name = 'AA000'
def initialize
#name = ##current_name
##current_name.next!
end
end
x = Robot.new
puts x.name
y = Robot.new
puts y.name
puts x.name == y.name
puts x.name
puts y.name
--output:--
AA001
AA002
true
AA002
AA002
Why is Ruby doing this
Because each instance's #name variable refers to the same String as the variable ##current_name, and you keep changing that String with the ! method.
what should I do to fix it
class Robot
attr_accessor :name
##current_name = 'AA000'
def initialize
#name = ##current_name.dup
##current_name.next!
end
end
x = Robot.new
puts x.name
y = Robot.new
puts y.name
p x.name == y.name
p x.name
p y.name
--output:--
AA000
AA001
false
AA000
AA001
Although, I and many other people will warn you NEVER to use ##variables in your code.
Ruby assignment operator:
1. x = “hello”:
x ------> “hello”
2. y = x:
x ------> “hello”
^
|
y -----------+
3. y << “ world”:
x ------> “hello world”
^ ^
| ^
y -----------+ ^
> > >
It matters not that x and y's names may be spelled #name and ##current_name.
Here is another code example:
x = "hello"
y = x
y << " world"
puts x, y
--output:--
hello world
hello world
x.next!
puts x, y
--output:--
hello worle
hello worle
Here is an example with immutable types:
1. x = 10:
x ------> 10
2. y = x:
x ---------> 10
^
|
y -----------+
3. y += 1
=> y = y + 1
=> y = 10 + 1
And 10 + 1 creates the new Integer object 11 and assigns it to y:
x ------> 10
y ------> 11
The expression 10 + 1 does NOT increment the Integer object 10 that both x and y refer to--because Integer objects are immutable.
Here is another example:
x = 10
y = x
x.next
puts x,y #=> ??
x.next creates a new Integer object 11, and because the newly created Integer object 11 is not assigned to a variable, 11 is discarded, so x and y still refer to the same Integer object 10.

Related

Is there a Ruby method for determining if all instance variables of two instances of the same class are equal?

Is there a Ruby method for comparing two objects based on whether all of their instance variables are equal? The method would behave like this code.
class Coordinates
attr_reader :x, :y
def initialize(x, y)
#x = x
#y = y
end
end
coordinates1 = Coordinates.new(0, 0)
coordinates2 = Coordinates.new(0, 0)
coordinates3 = Coordinates.new(1, 0)
compare(coordinates1, coordinates1) # => true
compare(coordinates1, coordinates2) # => true
compare(coordinates1, coordinates3) # => false
Does this method or something similar exist?
There is no built-in method for this, but you could quite easily write one. However, I think you're asking an XY question.
Here is what I think the question is supposed to say:
How should I define a method to check that two Coordinates instances are equal?
And here's my answer:
Define a custom == method:
class Coordinates
attr_reader :x, :y
def initialize(x, y)
#x = x
#y = y
end
def ==(other)
return super unless other.is_a?(Coordinates)
x == other.x && y == other.y
end
end
...But in the spirit of StackOverflow, here's some meta-programming to check whether all instance variables have the same name and value:
# returns true if all objects have the same instance variable names and values
def compare(*objects)
objects.map do |object|
object.instance_variables.map do |var_name|
[var_name, object.instance_variable_get(var_name)]
end
end.uniq.count == 1
end
Case 1
class A
def initialize(x,y)
#x = x
#y = y
end
def m
#x = 5
#y = 6
end
end
a1 = A.new(1,2)
#=> #<A:0x00005d22a3878048 #x=1, #y=2>
a1.m
a1 #=> #<A:0x00005d22a3878048 #x=5, #y=6>
a2 = A.new(3,4)
#=> #<A:0x00005d22a38b5330 #x=3, #y=4>
a2.m
a2 #=> #<A:0x00005d22a38b5330 #x=5, #y=6>
Then,
a1.instance_variables.all? { |e|
a1.instance_variable_get(e) == a2.instance_variable_get(e) }
#=> true
tells us that the values of #x and the values of #y are the same for both instances.
Case 2
Now let's change the code so that another instance variable is added conditionally.
class A
def initialize(x,y)
#x = x
#y = y
end
def m
#z = 3 if #x == 3
#x = 5
#y = 6
end
end
a1 = A.new(1,2)
#=> #<A:0x000057d1fd563c78 #x=1, #y=2>
a1.m
a1 #=> #<A:0x000057d1fd27f200 #x=5, #y=6>
a2 = A.new(3,4)
#=> #<A:0x000057d1fd57cb38 #x=3, #y=4>
a2.m
a2 #=> #<A:0x000057d1fd2f9e10 #x=5, #y=6, #z=3>
At this point are all instance variables of one of these instances equal to the corresponding instance variable of the other instance? No, because a2 has an additional instance variable, #z. Therefore,
a1.instance_variables.all? { |e|
a1.instance_variable_get(e) == a2.instance_variable_get(e) }
#=> true
gives the wrong answer, for obvious reasons. Perhaps we could test as follows:
a1.instance_variables.all? { |e|
a1.instance_variable_get(e) == a2.instance_variable_get(e) } &&
a2.instance_variables.all? { |e|
a1.instance_variable_get(e) == a2.instance_variable_get(e) }
#=> true && false => false
This has a gotcha, however, if #z equals nil.
Case 3
class A
def initialize(x,y)
#x = x
#y = y
end
def m
#z = nil if #x == 3
#x = 5
#y = 6
end
end
a1 = A.new(1,2)
#=> #<A:0x000057d1fd2d18e8 #x=1, #y=2>
a1.m
a1 #=> #<A:0x000057d1fd2d18e8 #x=5, #y=6>
a2 = A.new(3,4)
#=> #<A:0x000057d1fd46b460 #x=3, #y=4>
a2.m
a2
#=> #<A:0x000057d1fd46b460 #x=5, #y=6, #z=nil>
a1.instance_variables.all? { |e|
a1.instance_variable_get(e) == a2.instance_variable_get(e) } &&
a2.instance_variables.all? { |e|
a1.instance_variable_get(e) == a2.instance_variable_get(e) }
#=> true && true => true
We obtain this incorrect result because:
class A
end
A.new.instance_variable_get(:#z)
#=> nil
We therefore must confirm that if one instance has an instance variable named e, so does the other instance, and that each pair of instance variables with the same name are equal. One way to do that is as follows:
(a1.instance_variables.sort == a2.instance_variables.sort) &&
a1.instance_variables.all? { |e|
a1.instance_variable_get(e) == a2.instance_variable_get(e) }
#=> false && true => false
See Enumerable#all?, Object#instance_variables and Object#instance_variable_get.

Setting method local variables from a proc

If I have a class with two instance variables #x and #y, I can change them from a proc using self.instance_exec:
class Location
attr_accessor :x, :y
def initialize
#x = 0
#y = 0
end
def set &block
self.instance_exec(&block)
end
end
location = Location.new
location.set do
#x = rand(100)
#y = rand(100)
end
puts location.x
puts location.y
If I have a class with a method set with two local variables x and y, I can use proc return values to set them:
class Location
def set &block
x = 0;
y = 0;
x, y = block.call()
# do something with x and y
puts x
puts y
end
end
location = Location.new
location.set do
x = rand(100)
y = rand(100)
[x, y]
end
Is there a way to access the set method local variables x and y from the proc without using return values?
You can do it, sort of, but it isn't pretty
There is a way for block to set a variable in a calling method, but it isn't pretty. You can pass in a binding, then eval some code using the binding:
def foo(binding)
binding.eval "x = 2"
end
x = 1
foo(binding)
p x # => 2
Blocks also carry with them the binding in which they were defined, so if a block is being passed, then:
def foo(&block)
block.binding.eval "x = 2"
end
x = 1
foo {}
p x # => 2
What's in the block doesn't matter, in this case. It's just being used as a carrier for the binding.
Other ways to achieve the same goal
Yield an object
A more pedestrian way for a block to interact with it's caller is to pass an object to the block:
class Point
attr_accessor :x
attr_accessor :y
end
class Location
def set
point = Point.new
yield point
p point.x # => 10
p point.y # => 20
end
end
location = Location.new
location.set do |point|
point.x = 10
point.y = 20
end
This is often preferred to fancier techniques: It's easy to understand both its implementation and its use.
instance_eval an object
If you want to (but you probably shouldn't want to), the block's caller can use instance_eval/instance_exec to call the block. This sets self to the object, for that block.
class Location
def set(&block)
point = Point.new
point.instance_eval(&block)
p point.x # => 10
p point.y # => 20
end
end
location = Location.new
location.set do
self.x = 10
self.y = 20
end
You see that the block had to use use self. when calling the writers, otherwise new, local variables would have been declared, which is not what is wanted here.
Yield or instance_eval an object
Even though you still probably shouldn't use instance_eval, sometimes it's useful. You don't always know when it's good, though, so let's let the method's caller decide which technique to use. All the method has to do is to check that the block has parameters:
class Location
def set(&block)
point = Point.new
if block.arity == 1
block.call point
else
point.instance_eval(&block)
end
p point.x
p point.y
end
end
Now the user can have the block executed in the scope of the point:
location = Location.new
location.set do
self.x = 10
self.y = 20
end
# => 10
# => 20
or it can have the point passed to it:
location.set do |point|
point.x = 30
point.y = 40
end
# => 30
# => 40

Ruby find max number w/o running method twice

I want to find the max number without running the function twice
def foo(num)
num * 10
end
def bar
x = 0
for i in 0..5
if foo(i) > x
x = foo(i) # I don't want to run foo a second time
end
end
end
How about
def bar
(1..5).map{|i| foo(i)}.max
end
This will traverse 1 to 5, and max a new enumerable with foo(i) instead of i, then return the max.
If you want the value of x:
define_method(:foo) { |x| x * 10 }
(1..5).max_by { |x| foo(x) }
#=> 5
If you want the value of f(x):
(1..5).map { |x| foo(x) }.max
#=> 50
You can save the result of the function as a variable, so you can use it later without calling the function again.
Applied to your code example, it would look like this:
#...
fooOfI = foo(i)
if fooOfI > x
x = fooOfI
end
#...
Store the result of the method in a local variable.
def bar
x = 0
for i in 0..5
foo_result = foo i
if foo_result > x
x = foo_result
end
end
end
I would do some change in your code :
def foo(num)
num * 10
end
def bar
x = 0
for i in 0..5
_,x = [foo(i),x].sort #or you can write as x = [foo(i),x].max
end
x
end
p bar
# >> 50
Elegant and simple
foo = -> x { x * 10 }
(1..5).map(&foo).max
# => 50
In one iteration (no so elegant but performs better)
def foo(num); num * 10; end;
(1..5).reduce(-1.0/0) { |a, e| f = foo(e); f > a ? f : a }
# => 50

Breaking an until loop once condition is met in Ruby

For example in the following code, I would want the loop to end as soon as the condition evaluates as true
x = "B"
until x == "A"
x = gets.chomp
puts "x is not equal to "A""
end
So if the user enters "F" they get the puts but if they enter "A" then the puts does not get outputted.
x = true assigns true to x so until x = true is equivalent to until true.
So, replace = with == in the following line:
until x = true
->
until x == true
Or, it will never end.
UPDATE
Use following code:
while true
x = gets.chomp
break if x == 'A'
puts 'x is not equal to "A"'
end
or
until (x = gets.chomp) == 'A'
puts 'x is not equal to "A"'
end
The keyword break will exit from a loop.
x = false
a = 0
b = 0
until x # is a boolean already so no need for == true
a = 1
b = 2
# code here that could change state of x
break if x # will break from loop if x == anything other than false or nil
a = 2
b = 1
end
Obviously, this is not good code but there is some useful concepts in there that you might be able to fish out.
EDIT
In response to your new code, it is proper use for until loop.
puts "x is not equal to 'A'" until (x = gets.chomp) == "A"

question on overriding + operator in ruby

A recent convert to Ruby here. The following question isn't really practical; it's more of a question on how the internals of Ruby works. Is it possible to override the standard addition operator to accept multiple inputs? I'm assuming that the answer is no, given that the addition operator is a standard one, but i wanted to make sure i wasn't missing something.
Below is the code I wrote up quick to verify my thoughts. Note, it's completely trivial/contrived.
class Point
attr_accessor :x, :y
def initialize(x,y)
#x, #y = x, y
end
def +(x,y)
#x += x
#y += y
end
def to_s
"(#{#x}, #{#y})"
end
end
pt1 = Point.new(0,0)
pt1 + (1,1) # syntax error, unexpected ',', expecting ')'
You should not mutate the object when implementing + operator. Instead return a new Point Object:
class Point
attr_accessor :x, :y
def initialize(x,y)
#x, #y = x, y
end
def +(other)
Point.new(#x + other.x, #y + other.y)
end
def to_s
"(#{#x}, #{#y})"
end
end
ruby-1.8.7-p302:
> p1 = Point.new(1,2)
=> #<Point:0x10031f870 #y=2, #x=1>
> p2 = Point.new(3, 4)
=> #<Point:0x1001bb718 #y=4, #x=3>
> p1 + p2
=> #<Point:0x1001a44c8 #y=6, #x=4>
> p3 = p1 + p2
=> #<Point:0x1001911e8 #y=6, #x=4>
> p3
=> #<Point:0x1001911e8 #y=6, #x=4>
> p1 += p2
=> #<Point:0x1001877b0 #y=6, #x=4>
> p1
=> #<Point:0x1001877b0 #y=6, #x=4>
You can define the + method like that, but you'll only be able to call it using normal method call syntax:
pt1.+(1,1)
You can achieve something similar using arrays:
def +(args)
x, y = args
#x += x
#y += y
end
and later use it as:
pt1 + [1, 1]
You can also combine it with Chandra's solution, to accept both arrays and Points as arguments.

Resources