I'm attempting to use the ruby-alsa gem to provide audio playback on the server. Unfortunately, I keep getting an uninitialized constant MyClass::Playback exception when attempting to do so.
I'm very new to Ruby and Rails, so I'm not sure how to resolve this issue. The following has been added to my Gemfile and I have run a bundle install:
gem 'ruby-alsa'
My controller code looks like this (though I can't even begin to guarantee the validity of the code):
# Test audio playback
file = File.open("sample.wav")
#ALSA::PCM::Playback.open do |playback| # This line is commented out because it didn't work
Playback.open do |playback|
playback.write do |length|
file.read length
end
end
file.close
Update: If I uncomment the following line, I get the same exception (except ALSA is the uninitialized constant):
ALSA::PCM::Playback.open do |playback|
Looking at this briefly, it looks like your Gemfile needs to be:
gem 'ruby-alsa', :require => 'alsa'
It looks as if your commented-out code is correct; you should be using ALSA::PCM::Playback.
Your next problem is that write is an instance method of that class. As shown on the page you linked to, it appears that correct usage might be more like:
ALSA::PCM::Playback.open do |playback|
playback.write do |length|
file.read length
end
end
(Caveat: I know nothing about ALSA or this gem, so I have no idea what the above should do.)
Your code is inside a class so you need to do this:
::ALSA::PCM::Playback.open do |playback|
Note the preceding double colon.
Did you try to require it ?
require 'ruby-alsa'
edit:
Try to require rubygems first
require 'rubygems'
require 'ruby-alsa'
Related
I've recently run into a problem using OpenURI. Every open method results in the following error:
"No such file or directory # rb_sysopen".
My code looks simply like the following:
data = open("http://google.ca/")
I noticed the error shortly after adding gem 'nokogiri' to my Gemfile and running bundle install, though I have no indication of whether or not this caused the problem and have since removed the entry with no positive impact on the problem. Any help would be appreciated.
Try to write require 'open-uri' before your code.
I am using Ruby 3.0.1 and a part from the:
require "open-uri"
I have to explicitly call URI.open instead of just open:
data = URI.open("http://google.ca/")
Maybe it is something on new Ruby versions
I'm developing a gem/engine. The way I do this is by bundling it in a test RailsApp from source:
# Gemfile
gem 'my-engine', path: '../local/path/to/gem'
This works fine so far.
But, after I change a file in my gem (add a space or a break for example) the Engine is unloaded. Causing the following error:
uninitialized constant My::Engine
This error is thrown by the file that does the first call to My::Engine. ( I need to call that to get the root: My::Engine.root ) If I delete that line, there are no error thrown, but just an empty page is rendered, and this is happening because all my SQL's change and no content is loaded from the database. I think this is because the files in the lib dir are unloaded, because in these files I dynamically create active-record models..
I already checked out the autoload_paths and watchable_dirs:
# engine.rb
module My
class Engine < Rails::Engine
engine_name 'my-engine'
initializer "my-engine.load_config" do |app|
app.config.autoload_paths += %W(#{Engine.root}/lib)
app.config.watchable_dirs["#{Engine.root}/lib"] = [:rb]
end
end
end
I'm not sure if I'm implementing these the right way, but they don't seem to solve my problems the way I'm using them.
I think you may need to require 'my/engine' before calling My::Engine.root, or change the order of your requires so that 'my/engine' is required prior to the file that makes a call to My::Engine.
I have a problem when trying to include mixin from gem file to be used via console.
require "fertilizer/version"
module Fertilizer
# <-- CONSOLE EXTENSION (CONSOLE ONLY)-->
# Following part of code is active with the start of IRB console.
# Details about features can be seen in console_extensions module.
if defined?(Rails::Console)
require 'fertilizer/console_extensions'
include ConsoleExtensions
end
# <-- OBJECT EXTENSIONS (CONSOLE ONLY)-->
if defined?(Rails::Console)
require 'fertilizer/object_extensions'
end
end
Gem has ruby file fertilizer.rb which executes code above, when I start console I can see code being triggered. But when I try to use methods from mixin, console can't find them.
If I take exact same code and put it in initializer mixin gets loaded (as before) but this time I am able to use methods from mixin in consle.
How can I fix my gem file so that when gem loads, mixin methods are available via console?
More discussion can be found here:
How to automatically include Gem in the path?
Solution is:
Object.send(:include, self)
Not happy with solution but it works.
I have the simple following code, which is working in a ruby (not rails) app:
require 'gmail'
Gmail.new('my_account', 'my_password') do |gmail|
end
I am able to get a connection to the Gmail account and do some stuff in there.
However, I want to use this Gem in a Rails app, and therefore I have tried adding the following into the Gemfile:
gem "ruby-gmail", "0.2.1"
gem "mime", "0.1"
However, when I try to use this in a rake task, like this:
task :scrap_receipts_gmail => :environment do
Gmail.new('my_account', 'my_password') do |gmail|
puts gmail.inspect
end
end
I get the following error:
uninitialized constant Object::Gmail
This is solved if I add require 'gmail'. My question is:
Why would I have to require gmail, if I have already specified that in the Gemfile?
The module/class namespace has to match the directory structure. For example, in lib/foo/bar.rb, if and only if the namespace is Foo::Bar can it be auto loaded by Rails, otherwise you have to require it explicitly.
In this case, Gmail is defined as a class, which doesn't match the directory structure. If Gmail was defined as a module (namespace ::Gmail matchs directory structure), then you'll never need to explicitly require "gmail".
I'm really tired of typing my_ar_object.errors.full_messages in my console when i'm testing things...
So, I want to define this:
module ActiveRecord
class Base
def err
errors.full_messages
end
end
end
in my ~/.irbrc so that it is exclusive to script/console.
I don't want to define it in some rails initializer since I don't believe it belongs in the rails project (this is a irb helper)
The problem is, when I do that, this happens:
/.../gems/rails-2.3.5/lib/initializer.rb:437:in `initialize_database':NoMethodError: undefined method `configurations=' for ActiveRecord::Base:Class
Any ideas how I might make this work?
Did you load ActiveRecord in your .irbrc before defining the err method? Try adding
require 'active_record'
or
require 'rubygems'
gem 'activerecord', '2.3.5' # or whatever version you use
before defining the err method.
And another hint: irb looks for an .irbrc file in the current directory and in your home dir. So you could also craft a project-specific .irbrc in your project root directory. This way, you don't have to introduce ActiveRecord to your default irb config since it is a rather hefty dependency.