Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am creating a custom Trie as follows:
# frozen_string_literal: true
class CustomTrie
attr_accessor :trie
def initialize(items)
end
def self.parse_to_trie(items)
end
def get(path)
end
class Node
attr_accessor :key, :parent, :children,
def initialize(key: '', parent: nil, children: [])
# This isn't being called, why?
#key = key
#parent = parent
#children = children
end
def is_parent?
end
def is_leaf?
end
def inspect
{key: #key, parent: #parent, children: #children}
end
end
class Trie
attr_accessor :root
def initialize(root = Node.new)
#root = root
end
def add(path)
end
def get(path)
end
end
end
However when I try calling CustomTrie::Node.new everything is initialized to nil instead of the default values, and when I try calling the constructor with values I get the error: "ArgumentError (wrong number of arguments (given x, expected 0))"
I'm sure I'm missing something obvious, but I haven't been able to identify what I'm doing wrong.
:facepalm:
It turns out it was because I had a comma after :children in my attr_accessor call.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
"In my example, the initialize method expects to receive the two arguments. Once you associate an argument with the initialize method, you can not leave it off. If you do, it will generate an error." - so much for the theory but in my case it is the opposite ;(
class Player
attr_accessor :name, :age, :score
def inicialize(name, age, score)
#name = name
#age = age
#score = score
end
def to_s
"Player #{name} is #{age} old and have #{score} points"
end
end
So I call it simply:
player = Player.new("Name", "Surname", 25)
but the compiler responds:
`initialize': wrong number of arguments (given 3, expected 0) (ArgumentError)
So, than I do:
player = Player.new; player.name = 'Peter'
and it works.... why?
When I delete the arguments of inicialize it is the same exact story.
it should be initialize, not inicialize.
class Player
attr_accessor :name, :age, :score
def initialize(name, age, score)
#name = name
#age = age
#score = score
end
def to_s
"Player #{name} is #{age} old and have #{score} points"
end
end
player = Player.new("Name", "Surname", 25)
#=> #<Player:0x007fca2a4dfbb0 #age="Surname", #name="Name", #score=25>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Suppose I have:
class Parent
def foo
"foo"
end
end
class Child < Parent
class << self
end
Then
c = Child.new.foo
does not give me "foo". Why not?
The question above came when I was messing around trying to figure out why some code with the class << self idiom was behaving in a way I didn't understand. Since I had a syntax error in this post it is obvious that the error in the code I was looking at had nothing to do with class << self breaking inheritance. I want to delete this Q. Stack overflow won't let me. Hope to post a better question later.
There is a syntax error here. You never close the singleton class. Should be:
class Child < Parent
class << self
end
end
Works perfectly for me:
irb(main):008:0> class Parent
irb(main):009:1> def foo
irb(main):010:2> "foo"
irb(main):011:2> end
irb(main):012:1> end
=> :foo
irb(main):013:0> class Child < Parent
irb(main):014:1> class << self
irb(main):015:2> def foo
irb(main):016:3> "class-foo"
irb(main):017:3> end
irb(main):018:2> end
irb(main):019:1> end
=> :foo
irb(main):020:0> c = Child.new
=> #<Child:0x000001021eae30>
irb(main):021:0> c.foo
=> "foo"
irb(main):022:0> Child.foo
=> "class-foo"
class Child < Parent
class << self
end
This definition is syntactically invalid, and doesn't make sense. The correct definition is
class Child < Parent
class << self
end
end
But even in this case, it's completely useless unless you need to define something inside the singleton class. The following code is more than enough.
class Parent
def foo
"foo"
end
end
class Child < Parent
end
Here's an example
2.1.1 :010 > Child.new.foo
=> "foo"
You're missing an "end". class << self is not a one-liner, and requires you to close the block. I just ran your code with an extra end and it worked.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Let's consider this:
class Container
def function_one
...
end
def function_two
...
end
def function_three
...
end
attr_accessor :result_from_function_one
attr_accessor :result_from_function_two
attr_accessor :result_from_function_three
end
Since I can't create separate algorithm body for other classes, I created four separate classes. When I need to run algorithm one, I create a class with function one, and so on:
class Container
...
end
class ContainerWithFunctionOne < Container
def function_one
...
end
attr_accessor :result_from_function_one
end
class ContainerWithFunctionTwo < Container
def function_two
...
end
attr_accessor :result_from_function_two
end
class ContainerWithFunctionThree < Container
def function_three
...
end
attr_accessor :result_from_function_three
end
But when I combine function_one with function_two, I have an issue because they need to use the same data structure. So I was thinking about dividing the class Container into modules:
module FunctionOne
class Container
def function_one
...
end
attr_accessor :result_from_function_one
end
end
module FunctionTwo
class Container
def function_two
...
end
attr_accessor :result_from_function_two
end
end
module FunctionThree
class Container
def function_three
...
end
attr_accessor :result_from_function_three
end
end
But when I try to run it:
require_relative 'FunctionOne'
require_relative 'FunctionTwo'
require_relative 'FunctionThree'
containter = Container.new
container.function_one
container.function_two
container.function_three
it gave a run time error:
in `<top (required)>': uninitialized constant Container (NameError)
and I don't know how to fix this problem.
You can try
container = FunctionOne::Container.new
to create a new container
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm having the following error "Line 46: undefined local variable or method `app1' for main:Object (NameError)" when I run the following Ruby code about Methods and Classes on the compiler.Thanks in advance :D!!
class Apps
def initialize(name)
#name = name
end
def add_app
"#{name} has been added to the App Center.Approval is pending!!"
end
def app_approved
"#{name} has been approved by the App Center"
end
def app_posted
"Congratulations!!!!#{name} has been posted to the App Store."
end
end
class Fbapps
def initialize(name)
#name = name
#apps = []
end
def add_new(a_app)
#apps << a_app
"#{#app} has been added to the #{#apps} store!!"
end
def weekly_release
#apps.each do |app|
puts #app
end
#apps.each do |app|
app.add_app
app.app_approved
app.app_posted
end
end
end
apps = ["Bitstrip", "Candy Crush" , "Instapaper"]
apps = Fbapps.new("Apps")
apps.add_new(app1)
apps.add_new(app2)
apps.add_new(app3)
puts apps.weekly_release
app1 = Apps.new("Bitstrip")
app2 = Apps.new("Candy Crush")
app3 = Apps.new("Instapaper")
You need to create app1, app2, and app3 before adding them to apps:
apps = ["Bitstrip", "Candy Crush" , "Instapaper"]
app1 = Apps.new("Bitstrip")
app2 = Apps.new("Candy Crush")
app3 = Apps.new("Instapaper")
apps = Fbapps.new("Apps")
apps.add_new(app1)
apps.add_new(app2)
apps.add_new(app3)
puts apps.weekly_release
As noted there are other bugs in your classes, but they should be relatively trivial to fix given changing the order of execution as above.
Update: Here's your code updated to fix most of the bugs:
class Apps
attr_accessor :name
def initialize(name)
#name = name
end
def add_app
"#{name} has been added to the App Center.Approval is pending!!"
end
def app_approved
"#{name} has been approved by the App Center"
end
def app_posted
"Congratulations!!!! #{name} has been posted to the App Store."
end
end
class Fbapps
attr_accessor :name
def initialize(name)
#name = name
#apps = []
end
def add_new(a_app)
#apps << a_app
"#{a_app.name} has been added to the #{self.name} store!!"
end
def weekly_release
#apps.each do |app|
puts app.name
end
#apps.each do |app|
puts app.add_app
puts app.app_approved
puts app.app_posted
end
end
end
You're trying to do apps.add_new(app1) before you define app1. That line needs to go after app1 = Apps.new("Bitstrap") .
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have simple, how to return array with class instances ? I'm trying to return the array, but this variable return an empty array.
For example :
class Library
def initialize
##books = []
end
def all
##books
end
def add_book(arg = {})
#book = Book.new(arg)
##books << #book
end
end
class Book
attr_accessor :name, :year, :author, :content
def initialize( arg = {})
#name = arg[:name]
#year = arg[:year]
#author = arg[:author]
#content = arg[:content]
end
end
##books is a Library class variable. I am using method add_book to put books into #books, but how can i return array of these instances ? Sorry for bad english.
Thanks in advance !
When you call the method new to create a new object, ruby runs the initialize method. Since the initialize method sets ##books to an empty array, of course Library.new.all will return an empty array.
Class variables are shared by all instances of a class, so it doesn't make sense to be resetting it when you initialize a new Library as you'd be zeroing out the books stored in all other Library instances. From your usage it looks like you want a plain instance variable:
class Library
def initialize
#books = []
end
# you could replace this method with a `attr_reader :books`
def all
#books
end
# consider changing the method signature to accept a Book instance
def add_book(arg = {})
#books << Book.new(arg)
end
end