This question already has answers here:
In Ruby is there a way to overload the initialize constructor?
(7 answers)
Closed 9 years ago.
is there a way to have multiple “initialize” methods in ruby?
For example: one method excepting one argument while another excepts three ?
Something like
class One
def initialize (a)
puts a
end
def initialize_1 (a,b)
puts a ,b
end
end
initialize is actually not a constructor. You can indeed have two constructors.
class One
singletonclass.class_eval{alias old_new :new}
def self.new a
puts a
old_new
end
def self.new_1 a, b
puts a, b
old_new
end
end
Related
This question already has answers here:
Why do Ruby setters need "self." qualification within the class?
(3 answers)
Closed 4 years ago.
I am trying to understand the attr_accessor, and while digging I get pretty confused with the following behaviour:
class Item
def change_price
price=(2)
end
def price=(value)
#price = value
end
def price
#price
end
end
my_item = Item.new
p my_item.price
my_item.change_price
p my_item.price
=> nil
nil
I would expect the price to be set to 2. Clearly I totally misunderstood something that I thought obvious.
Would anybody be kind enough to explain me where I am being thick?
Thank you
Attribute setter (any function trailing with an equal sign) must be called on the explicit receiver. Otherwise, the local variable price is being created and assigned to the value.
Fix:
def change_price
# price=(2)
self.price=(2)
end
This question already has answers here:
Ruby: what does :: prefix do?
(3 answers)
Closed 7 years ago.
I know that the double colon (::) is basically a namespace resolution operator. But in this particular case, I'm not sure in which scope I'm working. Does it mean that I want MyClass class from the ruby core? Sort of like ~ means home directory in bash..
Imagine the following code:
class A
def a
puts 'TOPMOST'
end
end
module B
class A
def a
puts 'NESTED'
end
end
def self.topmost
::A.new.a
end
def self.nested
A.new.a
end
end
B.topmost will print "TOPMOST", and B.nested will print "NESTED".
So, ::A means not “from ruby core”, but rather “from no module.”
This question already has answers here:
Is it possible to define a Ruby singleton method using a block?
(2 answers)
Closed 7 years ago.
I have a container class Foo with a method frob, and I want to add a similarly named method, which will delegate to the container, to each of the contained elements.
First I tried
self.children.each do |c|
def c.frob
self.frob
end
end
but this of course leads to SystemStackError: stack level too deep, as self is c at that point. I then tried
parent = self
self.children.each do |c|
def c.frob
parent.frob
end
end
but local variables aren't part of the closure of the newly defined method, so I get undefined local variable or method 'parent'.
I came up with the following hack, which works:
self.children.each do |c|
c.instance_variable_set('#parent', self)
def c.frob
#parent.frob
end
end
However, it pollutes the variable space of the child with something that's only needed by this one method. How can I get parent/self in there while keeping the newly defined method self-contained?
This should work:
children.each do |c|
parent = self
c.send(:define_method, :frob) do
parent.frob
end
end
This question already has answers here:
How to call methods dynamically based on their name? [duplicate]
(5 answers)
Closed 8 years ago.
Is there a way to transform a symbol to a method call? I know it's possible for a String, but not for a symbol. I have this code in conveyance.rb:
def boats
boats = []
User.all.each{ |u| boats += u.boats}
boats
end
def boats_nb
self.boats.length
end
def cars
cars = []
User.all.each{ |u| cars += u.cars}
cars
end
def cars_nb
self.cars.length
end
I would like to know if there is a means to make a method like that:
def conveyances(:symb)
c = []
User.all.each{ |u| c += u.to_method(:symb) }
c
end
def conveyances_nb(:symb)
User.to_method(:symb).length
end
You could use Object#public_send method:
def conveyqnces_nb(symb)
User.public_send(symb).length
end
or, if you would like, Object#send might be suitable. The difference is that send allows to call private and protected methods, while public_send acts as typical method call - it raises an error if method is private or protected and the caller object isn't allowed to call it.
Use the #send instance method
u.send(:symb)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why isn't the eigenclass equivalent to self.class, when it looks so similar?
class << self idiom in Ruby
I have this class:
class Player < ActiveRecord::Base
before_save :set_slug
def remains
((end_date - Date.today) + 1).to_i
end
def self.url
"Our_link_#{slug}"
end
class << self
def load_track_lists
#do somthing
end
end
end
and I understand the class and instance methods but the
class << self
def load_track_lists
#do somthing
end
end
is really confusing. What is it and how does it differ from the prior two methods?
The end result is basically the same as if it had been defined as
def self.load_track_lists
#do somthing
end
There are subtle differences between the two methods if you're doing more than just defining methods in the class << self block, as described in the linked question, but effectively you're "opening up" the current class to define class level methods in it.
It doesn't differ from the self.url method. It's basically a container that allows you to put multiple methods without having to put self. in front of the method name. Probably not useful in the example but can be quite clean for other classes.
IMO it's a developer's preference