Tuesday, December 16, 2014

Increase limit of open files / IO in ruby

Get [current, maximum] value pair:
Process.getrlimit(:NOFILE)[1]
Set limit:
number = 1000
Process.setrlimit(:NOFILE, number)

Sunday, December 14, 2014

The operation can’t be completed because one or more required items can’t be found. (Error code -43)

In my case this error appear after some operations with File Vault when I tried to delete any file/folder using finder. Go to Disk utility. Select main drive (or wherever the error appear). There should be 2 active buttons. Use 'Verify Disk' and 'Repair disk'.

Tuesday, December 2, 2014

Unpack .dz file. read *.dict.dz, *.dict

.dz is just .bz file. It is Stardict dictionary data file. And inside it is XML file (looks like). To unpack run:
gunzip filename.dict.dz -S .dz
For ruby You could read it using GZip reader and process as xml using nokogiri.
require 'nokogiri'
require 'zlib'
include Zlib
data = GzipReader.open("dict.dict.dz"){|f|f.read}
doc = Nokogiri::XML(data)
p doc.xpath("k")[0].content


Monday, November 17, 2014

Chrome extension download file example


Some example of generating file from chrome extension.
logError: function(error) { console.log(error); }
downloadFile: function(format, file_name, data){
    if(!format){
      format = 'txt';
    }
    if(!file_name){
      file_name = 'default';
    }
    file_name = file_name + '.' + format;
    if(data.length == 0){
      return; // stupid stub
    }
    window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
    window.requestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
      fs.root.getFile(file_name, {create: true}, function(fileEntry) {
        fileEntry.createWriter(function(fileWriter) {
          var blob = new Blob([data]);
          fileWriter.addEventListener("writeend", function() {
            chrome.tabs.create({ url: fileEntry.toURL() });
          }, false);
          fileWriter.write(blob);
        }, logError);
      }, logError);
    }, logError);
  },

Sunday, October 26, 2014

Download Youtube video/audio


Download tool for grabbing data:
brew install youtube-dl
Update it:
sudo youtube-dl -U
If You want to have just mp3 files. Install tools for converting files from http://ffmpegmac.net/ Move all files to bin folder:
cd path_to_unpacked_zip_tools
sudo mv * /urs/local/bin
Get video file:
youtube-dl https://www.youtube.com/xyz
Get audio file:
youtube-dl https://www.youtube.com/xyz -x --audio-format=mp3

Thursday, September 4, 2014

Command pattern ruby


class Command
  attr_reader :name
  def initialize(name)
    @name = name
  end
  def run
    raise "Implement me"
  end
end

class MakeBold < Command
  def initialize(text)
    super("Make bold text: #{text}")
    @text = text
  end
  
  def run; puts "<b>#{@text}</b>"; end
end

class MakeItalic < Command
  def initialize(text)
    super("Make italic text: #{text}")
    @text = text
  end
  
  def run; puts "<i>#{@text}</i>"; end
end

class MakeSized < Command
  def initialize(text, size=14)
    super("Make text: #{text} to be size: #{size}")
    @text = text
    @size = size
  end
  
  def run; puts "<span style='font-size:#{@size}px;'>#{@text}</span>"; end
end

commands = []
commands << MakeBold.new("to be bold")
commands << MakeItalic.new("to be italic")
commands << MakeSized.new("to be sized", 20)
commands << MakeBold.new("other to be bold")

# show description for each command prior to execution
commands.each {|command| command.name }
# execute commands
commands.each {|command| command.run }

Output:
$ ruby command.rb 
<b>to be bold</b>
<i>to be italic</i>
<span style='font-size:20px;'>to be sized</span>
<b>other to be bold</b>

Wednesday, August 27, 2014

Iterator pattern ruby

Iterator is too obvious in ruby, so assume some theoretical example.
class Iterator
  def first; end
  def last; end
  def next; end
  def current; end
  def has_next?; end
end

class InfiniteNumbers < Iterator
  def initialize
    @current = 0
  end
  
  def first
    1
  end
  
  def last
    raise "Infinite number"
  end
  
  def next
    @current += 1
  end
  
  def current
    @current
  end
  
  def has_next?
    true
  end  
end

iterator = InfiniteNumbers.new
puts 100.times.collect { iterator.next }.join(',')