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'm trying to push an element to the end of this array and I get an error I don't understand.
main.rb:
require 'shop.rb'
so = Shop.new()
so.get(2)
so.get(1)
shop.rb
class Shop
def new()
#products = []
end
def get(product)
#products.push(product)
end
end
error:
NoMethodError: undefined method `push' for nil:NilClass
In Ruby, the constructor is initialize, not new. But you still use new to create an object, e.g. Checkout.new.
Also, the parentheses after method names are optional, and generally avoided in Ruby when there are no arguments.
def initialize
#items = []
end
Related
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
Is it possible in Ruby to define such setters and getters for class that can be used with [] or () or smth alike? E.g.
word.meaning[:english] = "ruby"
puts word.meaning[:german] # "Rubin"
Note that word.meaning must not be a hash! and :english, :german are kind of additional parameters for setter/getter meaning.
Yes, it can be done. You need to define a []= and [] methods.
In this example, I am using a Hash as the internal data structure - you are free to use whatever you like.
class Word
attr_reader :meaning
def initialize
#meaning = Meaning.new
end
end
class Meaning
attr_reader :h
def initialize
#h = {}
end
def []=(key, value)
#h[key] = value
end
def [](key)
#h[key]
end
end
Example:
word = Word.new
word.meaning[:english] = 'Hello'
word.meaning[:english] # => 'Hello'
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 made a class to show a dialog in sketchup, and the web dialog has methods such as show, close. In the initialize method, I return the web dialog object, and call:
$loginUI=LoginUI.new
$loginUI.show # it tell me no this method
Why can't I access the WebDialog method and get the object as the return value unless I rewrite that method as follows?
class LoginUI
##me=nil
def initialize()
##me=intiLoginDlg()
##me.show()
return ##me
end
def intiLoginDlg()
#dl = UI::WebDialog.new("aaa", true, "bbb", 50, 50, 0, 0, false);
#...do something
return #dl
end
################################# I must add this method to trigger it??
def isShow()
return ##me.visible?
end
def show()
##me.show
end
def close
##me.close
end
end
I'm not sure exactly what you are trying to do, but I think the magic you are looking for is simply this:
class LoginUI < UI::WebDialog
# initialize() and intiLoginDlg
end
If you do this, then:
$loginUI=LoginUI.new
$loginUI.show
...should work as expected.
Another much more advanced (and probably over-engineered!) solution that uses encapsulation instead of sub-classing would be to experiment with method_missing.
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 9 years ago.
Improve this question
I'm new to ruby and I'am building a little program on ruby alone,the problem is that I'am trying to launch it without success
Imagine that i have this code
#usr/bin/ruby
Class Whatever
def get_user_input
#user_input = gets.chomp
user_doing(#user_input)
end
def user_doing
#something
end
end
What I want is to call the get_user_input method as soon as i feed my rb file to ruby
I tried to call it on a initialize method
def initialize
get_user_input
end
I also tried to define it as a "class method"
def get_user_input
#user_input = gets.chomp
user_doing(#user_input)
end
but neither of them seems to work as when I'm start the rb file the program doesn't expect my input so how can i do this?.
You just define a class. What you did not call the method. Just add Whatever.new.get_user_input to your file.
#usr/bin/ruby
class Whatever
def initialize(input)
#input = input
end
def self.get_user_input
whatever = new(gets.chomp)
whatever.user_doing
end
def user_doing
puts "Input was: #{#input}"
end
end
Whatever.get_user_input
Btw: Your user_doing does not take args in the moment. You may want to check that.
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
How can I reference an object from a method without passing it as a variable?
I am getting error:
NameError: undefined local variable or method ‘my_object’ for Myclass:Class
class B
attr_accessor :somethings
include Enumerable
def initialize(*values)
self.somethings = []
end
end
my_object = B.new({d:'d'})
class Myclass
def self.my_method
p my_object
end
end
Run: Myclass.my_method
What I do NOT want to do for all my methods is...
def self.my_method(my_object)
p my_object
end
I could also solve the issue with use from a global variable
$my_object = B.new({d:'d'})
However, I only really want to make the variable available to my methods in Myclass class.
I only really want to make the variable available to my methods in Myclass class.
Then you should define it as an instance variable in corresponding scope of Myclass. As it is now, my_object is a local variable and is not visible because of the scope gate.
class Myclass
#my_object = B.new({d:'d'})
def self.my_method
p #my_object
end
end
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
What is "better" is Ruby
class BaseClass
def items
#items ||= get_items
end
def get_items
raise NotImplementedError
end
end
class ClildClass < BaseClass
def get_items
# ... fetching items...
end
end
or simply
class BaseClass
def items
#items ||= get_items
end
end
class ClildClass < BaseClass
def get_items
# ... fetching items...
end
end
?
It's up to you. There is no right answer for this. You will simply get 2 different errors. First coince should be better if someone else will implement other ChildClasses, because they can see the "interface" they need to implement on their BaseClass.