logo
down
shadow

Instance variable with EventMachine module


Instance variable with EventMachine module

Content Index :

Instance variable with EventMachine module
Tag : ruby , By : user179190
Date : December 01 2020, 05:00 PM

To fix the issue you can do Instead of using the module method, you can inherit EM::Connection and pass mpd through EventMachine.start_server, which will pass it onto the class' initialize method.
require 'ruby-mpd'
require 'eventmachine'

class RB3Jay < EM::Connection
  def initialize(mpd)
    @mpd = mpd
  end

  def receive_data
    # do stuff with @mpd
  end

  def self.start
    mpd = MPD.new
    mpd.connect

    EventMachine.run do
      EventMachine.start_server("127.0.0.1", 7331, RB3Jay, mpd)
    end
  end
end

RB3Jay.start

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

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?


Tag : ruby , By : James
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


Tag : python , By : Asbie
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
Related Posts Related QUESTIONS :
  • How do I pass arguments from the parent task to the child task in Rake?
  • Use ruby Timeout class as background thread
  • What are the features of dynamic languages (like Ruby or Clojure) which you are missing in Scala?
  • xapian-full installed on mac os x snow leopard but failed with dlopen LoadError
  • Xapian gem failed to install on Mac OS X Snow Leopard + macports
  • Shell Script Sequencing with Rake
  • What is the best way to format a date in JSON for Mongo DB storage
  • Hot deploy on Heroku with no downtime
  • "Personal" method in ruby
  • Advanced Array Sorting in Ruby
  • Markdown to plain text in Ruby?
  • How do I handle a missing mandatory argument in Ruby OptionParser?
  • aws-s3 and European Buckets
  • How to verify that "puts" has been called with a certain message?
  • Rack URL Mapping
  • Good and free Ruby editor for Windows?
  • Ruby TCPSocket doesn't notice it when server is killed
  • Merge a hash with the key/values of a string in ruby
  • Iterate through every file in one directory
  • How to validate SSL certificate chain in ruby with net/http
  • How can I upgrade my Ruby Gems to the latest version "safely"?
  • Reading a file N lines at a time in ruby
  • Package Ruby to a standalone Mac OS X application?
  • How can install sqlite-ruby on linux when sqlite3 is not in /usr/local?
  • Simple Serialization Faster Than JSON? (in Ruby)
  • Nokogiri Error: undefined method `radiobutton_with' - Why?
  • Ruby Pony alternative for Ruby 1.9?
  • How do I put an escape character (NOT "escaped" character) in a Ruby regex?
  • Lazy evaluation in Ruby
  • Net::HTTPResponse body as IO
  • Ruby DSL experiences?
  • Is there a way to return a method parameter names in ruby
  • Rack::Test not able to find web app cookie
  • rb_str_modify() equivalent in the Ruby language
  • How to make OSX application that just runs opens some file type and runs arbitrary Ruby code?
  • RVM Ruby 1.9.1 install can't locate zlib but its runtime and dev library are there
  • VSCode Style/PercentLiteralDelimiters rule not being respected in ruby files
  • What does it mean (and imply) that quicksort in Ruby is not stable?
  • Iv'e tried defining a function in ruby with constant parameter names, why does it return an error?
  • How to go through array of URLs using Curb
  • Ruby instance variables within class?
  • Capybara instance of "browser" for Page Object Model?
  • How to navigate to a link using page object gem?
  • undefined method `<' for true:TrueClass (NoMethodError)
  • AWS Inbound IP SSH Ranges to allow Capistrano deploy via Circle CI
  • During gem update I get permissions error for /usr/bin directory
  • Find element dict based on elements of a list
  • What to use instead of --no-ri for gem install?
  • Simple ruby script to process queue using multithreading throws Deadlock error?
  • How to sum values from csv file with Ruby
  • Is there a more elegant way of writing a while loop in Ruby where the array size is not known?
  • Why truncate when we had to pass a 'w' as an extra parameter to open in Ruby
  • Action on Unpermitted Parameters on production environment
  • Printing time remaining of the Time.now class to the console
  • Difference between {puts a} and {|a| puts a} in Ruby?
  • How to write a type signature for a block with Sorbet?
  • Creating a gem from a Capistrano task
  • Ruby spawn process, capturing STDOUT/STDERR, while behaving as if it were spawned regularly
  • RestClient post request in ruby on rails
  • Rubygems: You do not have permission to push to this gem
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com