stopping an infinite stream in elixir
Suppose you have an infinite stream. You can stop it as shown here
try do
for tweet <- ExTwitter.stream_filter(track: terms) do
process_tweet tweet
if logic_to_determine_halt?, do: throw :halt
end
catch
:halt -> #finished
end
migrating stuff from the commandline
Can be ran from the rails console:
ActiveRecord::Migration.remove_column(:users,:email)
listing all rails routes
Found myself needing a list of routes in an rspec controller test. This did the trick:
Rails.application.routes.routes.map{|e| e.path.spec.to_s }
no sound in ubuntu 15.04 skype
Had no sound after a fresh install, and everything in skype was showing virtual device. This is what fixed it:
sudo apt-get install pulseaudio
sudo apt-get install libpulse0:i386
test that rails controller rendered a specific layout
get :index
expect(response).to have_rendered(layout: 'main')
anonymous controllers with rspec
If you have new routes in your anonymous controller, the docs say you should do this:
require "rails_helper"
RSpec.describe ApplicationController do
controller do
def custom
render :text => "custom called"
end
end
specify "a custom action can be requested if routes are drawn manually" do
routes.draw { get "custom" => "anonymous#custom" }
get :custom
expect(response.body).to eq "custom called"
end
end
However, in my case, the following change helped:
specify "a custom action can be requested if routes are drawn manually" do
routes.draw { get "custom", controller: 'application_controller' }
get :custom
expect(response.body).to eq "custom called"
end
My First Video Post on Jekyll
Steve Austin: Astronaut. A man barely alive. Gentlemen… we can rebuild him. We have the technology. We have the capability to make the world’s first bionic man. Steve Austin will be that man. Better than he was before. Better. Stronger. Faster.
My name is Rhoda Morgenstern. I was born in the Bronx, New York in December of 1941. I’ve always felt responsible for World War II. The first thing I remember liking that liked me back was food. I had a bad puberty; it lasted seventeen years. I’m a high school graduate. I went to art school. My entrance exam was on a book of matches. I decided to move out of the house when I was twenty-four. My mother still refers to this as the time I ran away from home. Eventually, I ran to Minneapolis, where it’s cold and I figured I’d keep better. Now I’m back in Manhattan. New York, this is your last chance!
Dr. David Banner: physician; scientist. Searching for a way to tap into the hidden strengths that all humans have… then an accidental overdose of gamma radiation alters his body chemistry. And now when David Banner grows angry or outraged, a startling metamorphosis occurs. The creature is driven by rage and pursued by an investigative reporter. The creature is wanted for a murder he didn’t commit. David Banner is believed to be dead, and he must let the world think that he is dead, until he can find a way to control the raging spirit that dwells within him.
My First Post on Jekyll
Steve Austin: Astronaut. A man barely alive. Gentlemen… we can rebuild him. We have the technology. We have the capability to make the world’s first bionic man. Steve Austin will be that man. Better than he was before. Better. Stronger. Faster.
My name is Rhoda Morgenstern. I was born in the Bronx, New York in December of 1941. I’ve always felt responsible for World War II. The first thing I remember liking that liked me back was food. I had a bad puberty; it lasted seventeen years. I’m a high school graduate. I went to art school. My entrance exam was on a book of matches. I decided to move out of the house when I was twenty-four. My mother still refers to this as the time I ran away from home. Eventually, I ran to Minneapolis, where it’s cold and I figured I’d keep better. Now I’m back in Manhattan. New York, this is your last chance!
Dr. David Banner: physician; scientist. Searching for a way to tap into the hidden strengths that all humans have… then an accidental overdose of gamma radiation alters his body chemistry. And now when David Banner grows angry or outraged, a startling metamorphosis occurs. The creature is driven by rage and pursued by an investigative reporter. The creature is wanted for a murder he didn’t commit. David Banner is believed to be dead, and he must let the world think that he is dead, until he can find a way to control the raging spirit that dwells within him.
intro to celluloid
I started to use Celluloid to speed up some tasks, and I like the simplicity it offers.
For my specific case, I wanted to speed up some downloads/uploads. Here’s how you do it:
class YourClass
include Celluloid
def do_something
# code that takes a long time goes here
end
end
So, if we do this:
YourClass.new.do_something
will not use Celluloid the Ruby main thread becomes an Actor.
When you write YourClass.new.do_something you are actually doing a blocking call and invoking do_something
on the YourClass instance (which is contained within an actor). Thanks for the correction Sam Williams
YourClass.new.future.do_something
This returns a future, which may or may not be completed at the time you need it. Each future exposes a method called ready? which you can call to see if the future finished.
So, you could do something like this:
ftr = YourClass.new.future.do_something
loop do
break if ftr.ready?
sleep 0.1
end
puts ftr.value
Now, imagine that you want to run your processing using multiple threads at the same time. You could create a number of futures inside a loop, and then check them all with ready? and append to them once some of them have finished. I did that in the beginning as well :) Or, you could just use the pool method that Celluloid offers. So, to parallelize the class above, we can do this:
pool = YourClass.pool(size: 4) # number of threads in the pool
4.times {|e| pool.future.do_something(e) }
sleep 60 # or whatever time you estimate you need to complete all jobs
And the code above will queue 4 jobs to your pool. Notice that we’re sleeping for some seconds, to allow the jobs to finish. Yeah, I’m not a big fan of this approach either, because your jobs may take much longer, or much less than 60 seconds.
Luckily, there’s another class from Celluloid that comes to our rescue. Enter Celluloid::Condition:
pool = YourClass.pool(size: 4)
number_of_jobs = 100 # or however many you may have
condition = Celluloid::Condition.new
# do work
# do more work
# when you are done, call condition.signal
condition.wait # waits until someone calls signal on it
I know this was quite a whirlwind introduction to Celluloid. I invite you to experiment with it for a bit. I really like the power it offers you, and I’m quite sure you will find it useful too. As I learn more about it, I’ll try to post more updates/tips/tricks.