ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:99405] [Ruby master Bug#17093] attr_accessor works strange
@ 2020-07-30 20:50 mpavel
  2020-07-30 21:18 ` [ruby-core:99406] " merch-redmine
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: mpavel @ 2020-07-30 20:50 UTC (permalink / raw)
  To: ruby-core

Issue #17093 has been reported by mpavel (pavel m).

----------------------------------------
Bug #17093: attr_accessor works strange
https://bugs.ruby-lang.org/issues/17093

* Author: mpavel (pavel m)
* Status: Open
* Priority: Normal
* ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin17]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
require 'rubygems'

class A
  def initialize(type:)
    @type = type
  end

  def b
    p type
    p type.nil?
    type = 'default' if type.nil?
    type
  end

  private

  attr_accessor :type
end

RSpec.describe A do
  let(:type) { 'whoaaa' }

  it 'return default' do
    expect(A.new(type: type).b).to eq('default')
  end

  it 'instance variable is "whoaaa"' do
    expect(A.new(type: type).instance_variable_get(:@type)).to eq(type)
  end
end

all tests green

output
A
"whoaaa"
false
  return default
  instance variable is "whoaaa"



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:99406] [Ruby master Bug#17093] attr_accessor works strange
  2020-07-30 20:50 [ruby-core:99405] [Ruby master Bug#17093] attr_accessor works strange mpavel
@ 2020-07-30 21:18 ` merch-redmine
  2020-07-30 22:31 ` [ruby-core:99408] " mpavel
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: merch-redmine @ 2020-07-30 21:18 UTC (permalink / raw)
  To: ruby-core

Issue #17093 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Open to Feedback

Do you expect the `type = 'default'` in `b` to set the @type instance variable to `'default'`?  If so, that doesn't work, you need to use `self.type =` instead of `type =`.  If not, can you provide an explanation of what behavior you are expecting? 

----------------------------------------
Bug #17093: attr_accessor works strange
https://bugs.ruby-lang.org/issues/17093#change-86854

* Author: mpavel (pavel m)
* Status: Feedback
* Priority: Normal
* ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin17]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
require 'rubygems'

class A
  def initialize(type:)
    @type = type
  end

  def b
    p type
    p type.nil?
    type = 'default' if type.nil?
    type
  end

  private

  attr_accessor :type
end

RSpec.describe A do
  let(:type) { 'whoaaa' }

  it 'return default' do
    expect(A.new(type: type).b).to eq('default')
  end

  it 'instance variable is "whoaaa"' do
    expect(A.new(type: type).instance_variable_get(:@type)).to eq(type)
  end
end

all tests green

output
A
"whoaaa"
false
  return default
  instance variable is "whoaaa"



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:99408] [Ruby master Bug#17093] attr_accessor works strange
  2020-07-30 20:50 [ruby-core:99405] [Ruby master Bug#17093] attr_accessor works strange mpavel
  2020-07-30 21:18 ` [ruby-core:99406] " merch-redmine
@ 2020-07-30 22:31 ` mpavel
  2020-07-30 22:41 ` [ruby-core:99409] " merch-redmine
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: mpavel @ 2020-07-30 22:31 UTC (permalink / raw)
  To: ruby-core

Issue #17093 has been updated by mpavel (pavel m).


seriously? 
attr_accesor doesnot provide value?

----------------------------------------
Bug #17093: attr_accessor works strange
https://bugs.ruby-lang.org/issues/17093#change-86856

* Author: mpavel (pavel m)
* Status: Feedback
* Priority: Normal
* ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin17]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
require 'rubygems'

class A
  def initialize(type:)
    @type = type
  end

  def b
    p type
    p type.nil?
    type = 'default' if type.nil?
    type
  end

  private

  attr_accessor :type
end

RSpec.describe A do
  let(:type) { 'whoaaa' }

  it 'return default' do
    expect(A.new(type: type).b).to eq('default')
  end

  it 'instance variable is "whoaaa"' do
    expect(A.new(type: type).instance_variable_get(:@type)).to eq(type)
  end
end

all tests green

output
A
"whoaaa"
false
  return default
  instance variable is "whoaaa"



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:99409] [Ruby master Bug#17093] attr_accessor works strange
  2020-07-30 20:50 [ruby-core:99405] [Ruby master Bug#17093] attr_accessor works strange mpavel
  2020-07-30 21:18 ` [ruby-core:99406] " merch-redmine
  2020-07-30 22:31 ` [ruby-core:99408] " mpavel
@ 2020-07-30 22:41 ` merch-redmine
  2020-07-31  6:27 ` [ruby-core:99412] " mpavel
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: merch-redmine @ 2020-07-30 22:41 UTC (permalink / raw)
  To: ruby-core

Issue #17093 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Feedback to Closed

In Ruby `type = 'default'` only sets a local variable named `type`.  It does not call the `type=` method on self, you need `self.type = 'default'` for that.

See https://ruby-doc.com/docs/ProgrammingRuby/html/tut_expressions.html, specifically the section on "Sidebar: Using Accessors Within a Class".

----------------------------------------
Bug #17093: attr_accessor works strange
https://bugs.ruby-lang.org/issues/17093#change-86857

* Author: mpavel (pavel m)
* Status: Closed
* Priority: Normal
* ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin17]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
require 'rubygems'

class A
  def initialize(type:)
    @type = type
  end

  def b
    p type
    p type.nil?
    type = 'default' if type.nil?
    type
  end

  private

  attr_accessor :type
end

RSpec.describe A do
  let(:type) { 'whoaaa' }

  it 'return default' do
    expect(A.new(type: type).b).to eq('default')
  end

  it 'instance variable is "whoaaa"' do
    expect(A.new(type: type).instance_variable_get(:@type)).to eq(type)
  end
end

all tests green

output
A
"whoaaa"
false
  return default
  instance variable is "whoaaa"



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:99412] [Ruby master Bug#17093] attr_accessor works strange
  2020-07-30 20:50 [ruby-core:99405] [Ruby master Bug#17093] attr_accessor works strange mpavel
                   ` (2 preceding siblings ...)
  2020-07-30 22:41 ` [ruby-core:99409] " merch-redmine
@ 2020-07-31  6:27 ` mpavel
  2020-07-31  7:34 ` [ruby-core:99414] " hanmac
  2020-07-31  8:31 ` [ruby-core:99416] " mpavel
  5 siblings, 0 replies; 7+ messages in thread
From: mpavel @ 2020-07-31  6:27 UTC (permalink / raw)
  To: ruby-core

Issue #17093 has been updated by mpavel (pavel m).


you doesnt see "if type.nil?" its local variable?
p type.nil? return false
next "if type.nil?" return true and "type = 'default'" will work
why?

----------------------------------------
Bug #17093: attr_accessor works strange
https://bugs.ruby-lang.org/issues/17093#change-86860

* Author: mpavel (pavel m)
* Status: Closed
* Priority: Normal
* ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin17]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
require 'rubygems'

class A
  def initialize(type:)
    @type = type
  end

  def b
    p type
    p type.nil?
    type = 'default' if type.nil?
    type
  end

  private

  attr_accessor :type
end

RSpec.describe A do
  let(:type) { 'whoaaa' }

  it 'return default' do
    expect(A.new(type: type).b).to eq('default')
  end

  it 'instance variable is "whoaaa"' do
    expect(A.new(type: type).instance_variable_get(:@type)).to eq(type)
  end
end

all tests green

output
A
"whoaaa"
false
  return default
  instance variable is "whoaaa"



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:99414] [Ruby master Bug#17093] attr_accessor works strange
  2020-07-30 20:50 [ruby-core:99405] [Ruby master Bug#17093] attr_accessor works strange mpavel
                   ` (3 preceding siblings ...)
  2020-07-31  6:27 ` [ruby-core:99412] " mpavel
@ 2020-07-31  7:34 ` hanmac
  2020-07-31  8:31 ` [ruby-core:99416] " mpavel
  5 siblings, 0 replies; 7+ messages in thread
From: hanmac @ 2020-07-31  7:34 UTC (permalink / raw)
  To: ruby-core

Issue #17093 has been updated by Hanmac (Hans Mackowiak).


This:

``if type.nil?
 type = 'default'
end``

is different from this:

`type = 'default' if type.nil?`

because of the lexical reading, it reads the setting of local variable first and now assumes that type is local variable in this case

----------------------------------------
Bug #17093: attr_accessor works strange
https://bugs.ruby-lang.org/issues/17093#change-86861

* Author: mpavel (pavel m)
* Status: Closed
* Priority: Normal
* ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin17]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
require 'rubygems'

class A
  def initialize(type:)
    @type = type
  end

  def b
    p type
    p type.nil?
    type = 'default' if type.nil?
    type
  end

  private

  attr_accessor :type
end

RSpec.describe A do
  let(:type) { 'whoaaa' }

  it 'return default' do
    expect(A.new(type: type).b).to eq('default')
  end

  it 'instance variable is "whoaaa"' do
    expect(A.new(type: type).instance_variable_get(:@type)).to eq(type)
  end
end

all tests green

output
A
"whoaaa"
false
  return default
  instance variable is "whoaaa"



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [ruby-core:99416] [Ruby master Bug#17093] attr_accessor works strange
  2020-07-30 20:50 [ruby-core:99405] [Ruby master Bug#17093] attr_accessor works strange mpavel
                   ` (4 preceding siblings ...)
  2020-07-31  7:34 ` [ruby-core:99414] " hanmac
@ 2020-07-31  8:31 ` mpavel
  5 siblings, 0 replies; 7+ messages in thread
From: mpavel @ 2020-07-31  8:31 UTC (permalink / raw)
  To: ruby-core

Issue #17093 has been updated by mpavel (pavel m).


are you try this solution?

```
if type.nil?
 type = 'default'
end
```


----------------------------------------
Bug #17093: attr_accessor works strange
https://bugs.ruby-lang.org/issues/17093#change-86867

* Author: mpavel (pavel m)
* Status: Rejected
* Priority: Normal
* ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin17]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
```ruby
require 'rubygems'

class A
  def initialize(type:)
    @type = type
  end

  def b
    p type
    p type.nil?
    type = 'default' if type.nil?
    type
  end

  private

  attr_accessor :type
end

RSpec.describe A do
  let(:type) { 'whoaaa' }

  it 'return default' do
    expect(A.new(type: type).b).to eq('default')
  end

  it 'instance variable is "whoaaa"' do
    expect(A.new(type: type).instance_variable_get(:@type)).to eq(type)
  end
end
```

all tests green

```
output
A
"whoaaa"
false
  return default
  instance variable is "whoaaa"
```



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-07-31  8:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-30 20:50 [ruby-core:99405] [Ruby master Bug#17093] attr_accessor works strange mpavel
2020-07-30 21:18 ` [ruby-core:99406] " merch-redmine
2020-07-30 22:31 ` [ruby-core:99408] " mpavel
2020-07-30 22:41 ` [ruby-core:99409] " merch-redmine
2020-07-31  6:27 ` [ruby-core:99412] " mpavel
2020-07-31  7:34 ` [ruby-core:99414] " hanmac
2020-07-31  8:31 ` [ruby-core:99416] " mpavel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).