My class is
class LineAnalyzer
attr_reader :highest_wf_words,:highest_wf_count,:content,:line_number
def initialize(content ="",line_number)
#content = content
#line_number = line_number
end
def calculate_word_frequency
end
end
When I make object of class like this
l = LineAnalyzer.new("", 2)
l.calculate_word_frequency
It gives me this error.HOw to avoid this?
Related
I am ruby beginner. I am use proc class but I am getting error.
class Timeline
attr_accessor :tweets
def each(&block) # Block into the proc
tweets.each(&block) # proc back into the block
end
end
timeline = Timeline.new(tweets)
timeline.each do |tweet|
puts tweet
end
Getting error :-
`each': undefined method `each' for nil:NilClass (NoMethodError)
How to solve this error? Please tell us!
When you define attr_accessor :tweets, you just define 2 instance methods:
def tweets
#tweets
end
def tweets=(tweets)
#tweets = tweets
end
When you call tweets inside the each method, you just call method with this name, not a local variable, so you should set #tweets in the initialize method because right now your #tweets variable is not set:
class Timeline
attr_accessor :tweets # this is just a nice syntax for instance variable setter
# and getter
def initialize(tweets)
#tweets = tweets
end
def each(&block) # Block into the proc
tweets.each(&block) # proc back into the block
end
end
I am trying to access class methods within a define_singleton_method block, but it doesn't seem to be working.
Here is an example.
class Test
attr_accessor :tags
def initialize
#tags = []
#tags.define_singleton_method(:<<) do |val|
val = tosymbol(val)
push(val)
end
end
def tosymbol(value)
value = value.to_s
value = value.gsub!(/\s+/,'_') || value
value = value.downcase! || value
return value.to_sym
end
end
But when I use it I get an error.
test = Test.new
test.tags<<"Hello World"
NoMethodError: undefined method `tosymbol' for []:Array
from /home/joebloggs/GitHub/repo/file.rb:183:in `block in initialize'
from (irb):9
from /home/joebloggs/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
I tried changing val = tosymbol(val) to val = Test::tosymbol(val) but that didn't work either, I get undefined method 'tosymbol' for Test:Class
I could re-write what tosymbol is doing, but it wouldn't be very DRY. Where am I going wrong? Thanks.
Where am I going wrong?
You're (re)defining a << method for instance of Array class, not Test class.
While doing so you are trying to access tosymbol method, that is not defined in Array class, but in Test class.
What you want, probably (read judging by your code sample), is to define << method for instances of Test class:
def initialize
#tags = []
end
def <<(val)
tags << tosymbol(val)
end
test = Test.new
test << "Hello World"
#=> [:hello_world]
EDIT
To make your example to work you just need to assign the instance to a variable and call the tosymbol method with correct receiver:
def initialize
#tags = []
test = self # <============
#tags.define_singleton_method(:<<) do |val|
val = test.tosymbol(val)
push(val)
end
end
Now:
test.tags << 'Hello World'
#=> [:hello_world]
I have the following code:
class Derp
#state = Hash.new
def run
#state[:ran] = true
end
end
derp = Derp.new
derp.run
Which results in the following error:
NoMethodError: undefined method `[]=' for nil:NilClass
from (irb):4:in `run'
from (irb):8
from /usr/local/bin/irb:11:in `<main>'
I'm pretty new to Ruby, so I'm not sure exactly what's going on here. Anyone have any ideas?
class Derp
def initialize
#state = Hash.new
end
def run
#state[:ran] = true
end
end
derp = Derp.new
derp.run
The problem in your code is that in the way you did the hash is assigned to the instance variable #state of the class object Derp, not to Derp's objects. Instance variables of the class are different from instance variables of that class’s objects. You could use that variable in class methods. E.g.
class Derp
#state = 42
def self.state
#state
end
end
puts Derp.state # 42
I have this class:
class Game
attr_accessor :player_fleet, :opponent_fleet
#player_fleet = []
#opponent_fleet = []
...
end
and create an instance like this:
my_game = Game.new
then use it like this:
my_game.opponent_fleet << opponent
which gives me this error:
undefined method `<<' for nil:NilClass (NoMethodError)
Why can't I treat an array like this? Do I have to create a method to push objects into the array?
You initialize #opponent_fleet at class level, so it's an instance variable of the class, not of the generated objects. Remember that in Ruby, even classes are objects :)
irb(main):001:0> class Game
irb(main):002:1> #foo = 3
irb(main):003:1> end
irb(main):004:0> Game.instance_eval { #foo }
=> 3
irb(main):005:0> Game.new.instance_eval { #foo }
=> nil
You want to initialize it in a constructor instead:
class Game
attr_accessor :player_fleet, :opponent_fleet
def initialize
#player_fleet = []
#opponent_fleet = []
end
end
I am hoping that someone could shed some light on the error that I am receiving below. I define an instance variable in the parent class Node and want to access and modify it in the subclass AddSubNode, whenever I try to access #code I receive this error:
'code': undefined method `<<' for nil:NilClass (NoMethodError)
I must be misunderstanding Ruby's inheritance model, but I thought that I could do this.
class Node
attr_accessor :code
def initialize
#code = []
end
end
class AddSubNode < Node
def initialize op, l, r
#op = op
#l = l
#r = r
end
def code
#code << 1 # error: `code': undefined method `<<' for nil:NilClass (NoMethodError)
#code
end
def to_s
"#{#l} #{#op} #{#right}"
end
end
You need to call the super initializer in the initializer of the subclass.
class AddSubNode < Node
def initialize op, l, r
super()
#op = op
#l = l
#r = r
end
...
edit: forgot parenthesis
When you redefine the initialize method in the subclass, you overwrite the original. Hence the instance variable #code is never initialized, and you code throws an error when you call #code << 1.
Calling super() from the initialize method in your subclass (effectively calling it's parent) or utilizing #code << 1 unless #code.nil? are a few ways to address the error.
Here I just tried to give you some visualization to test such scenarios.
class Node
attr_accessor :code
def initialize
#code = []
end
end
class AddSubNode < Node
def initialize op, l, r
#op = op
#l = l
#r = r
end
def code
#code << 1 # error: `code': undefined method `<<' for nil:NilClass (NoMethodError)
#code
end
end
ob = AddSubNode.new(1,2,3)
p ob.instance_variables #=> [:#op, :#l, :#r]
p ob.instance_variable_defined?(:#code) #=> false
p ob.instance_variable_set :#code,[12] #=> [12]
p ob.instance_variable_defined?(:#code) #=> true
p ob.instance_variable_get :#code #=> [12]
p ob.instance_variables #=> [:#op, :#l, :#r, :#code]
p ob.code #=> [12, 1]