ruby-eventmachine use start_server with an instance
Tag : ruby , By : christiandsg
Date : March 29 2020, 07:55 AM
I hope this helps you . As I say there was something that I was missing. You can add parameters for class to be instantiated : class Server< EM::Connection
def initialize par
puts "I'm server number#{par}"
end
def receive_data data
send_data data
close_connection_after_writing
end
end
EventMachine.run {
EventMachine.start_server '127.0.0.1','8080', Server,1
}
EventMachine.run {
EventMachine.start_server '127.0.0.1','8080', Server,2
}
|
Why can't I puts an instance variable of a Ruby class if it contains the value of a variable in a module?
Date : March 29 2020, 07:55 AM
should help you out Because @myg is not shared variable. It is private property of module Test, thus while you included Test, @myg didn't come into Xy due to the mixin, it wouldn't come also by default. But, "Why nil?" - Because, instance variable, class variables are like that. Before initialising/defining them, if you attempt to use them, it will simply give you nil. Small program to prove myself and Ruby :- module Test
@x = 10
@@y = 11
end
class Foo
include Test
end
Foo.instance_variable_defined?(:@x) # => false
Test.instance_variable_defined?(:@x) # => true
Foo.class_variable_defined?(:@@y) # => true
Test.class_variable_defined?(:@@y) # => true
module Test
class << self
attr_reader :myg
end
RED = "rose"
BLUE = "ivy"
@myg = 9
@@vit = 24.6
end
class Xy
include Test
def initialize(n)
@garb = n
end
attr_accessor :garb
def garbTest
@garb = Test.myg
end
def exo
return 50
end
end
ryu = Xy.new(16)
ryu.garbTest # => 9
|
How to access instance variable of a module from abother module?
Tag : ruby , By : mhedberg
Date : March 29 2020, 07:55 AM
like below fixes the issue Instance variables are called instance variables, because they belong to objects (aka "instances"). There are two objects in your example: SomeModule and Reporter. Both have their own instance variables. SomeModule has an instance variable named @param and Reporter has an instance variable named @param. Those two instance variables are completely unrelated, even though the have the same name. Imagine the chaos, if every object had to come up with its own names for instance variables! module SomeModule
def self.param; @param end
@param = 'Hello'
end
module Reporter
def self.put
puts SomeModule.param
end
end
module SomeModule
class << self; attr_reader :param end
@param = 'Hello'
end
module Reporter
def self.put
puts SomeModule.param
end
end
module SomeModule
class << self
attr_reader :param
private
def param=(param)
@param = param
end
end
self.param = 'Hello'
end
module Reporter
def self.put
puts SomeModule.param
end
end
module SomeModule
class << self
attr_reader :param
private
attr_writer :param
end
self.param = 'Hello'
end
module Reporter
def self.put
puts SomeModule.param
end
end
|
Assign module method to a Class variable or Instance variable
Date : March 29 2020, 07:55 AM
Any of those help Because you mapped a function as unbound method of A, so when you are calling cls.func you first ask something equals to getattr(cls, 'func') which returns BUT, this unbound method needs to be called with class as first argument. So because in this specific case cls.func means "gives me class attribute func of cls" it can not means to the same time "call class method func" - So Python doesn't translate cls.func() by func(cls).@classmethod
def test(cls):
print getattr(cls, 'func') # <unbound method A.task>
def task(cls=None):
if cls is None:
print 'task()'
else:
print 'A.foo({})'.format(cls)
a = task
class A:
func = task # this show error unbound method
def __init__(self):
self.func_1 = task
def test_1(self):
self.func_1()
@classmethod
def test(cls):
cls.func(cls())
a()
A().test_1()
A.test()
task()
task()
A.foo(<__main__.A instance at 0x7fd0310a46c8>)
|
Why is an instance variable used instead of a local variable in a module?
Tag : ruby , By : wiznick
Date : March 29 2020, 07:55 AM
it should still fix some issue This roughly means that if @current_user, as an instance variable of global request/response scope (Controller/View/Helpers), has already been defined, use it as current_user. Otherwise, assign it the user that corresponds to the id stored in the session do what you do. In other words, this is similar to: def current_user
if @current_user # this will be false in every hit, for the same request
@current_user
else
User.find_by id: session[:user_id]
end
end
def current_user
if @current_user # this will be false for the first hit, true for every subsequent request
@current_user
else
@current_user = User.find_by id: session[:user_id]
end
end
def current_user
@current_user ||= User.find_by id: session[:user_id]
end
|