`
fireflyman
  • 浏览: 113347 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

ruby各种小脚本(集装箱)

    博客分类:
  • ROR
阅读更多
有时候听歌听的入迷了,就想下载google的歌词,可是下下来总是这个样子...
引用
[00:17.00]有时后我觉得自己像一只小小鸟
[00:23.00]想要飞 却怎么样也飞不高
[00:30.00]也许有一天我栖上枝头 却成为猎人的目标
[00:36.00]我飞上了青天才发现自己从此无依无靠
[00:43.00]每次到了夜深人静的时候我总是睡不着
[00:50.00]我怀疑是不是只有我的明天没有变得更好
[00:56.00]未来会怎样究竟有谁会知道
[01:03.00]幸福是否只是一种传说 我永远都找不到
[01:10.00]我是一只小小小小鸟 想要飞呀飞却飞也飞不高
[01:24.00]我寻寻觅觅寻寻觅觅一个温暖的怀抱
[01:29.00]这样的要求算不算太高
[01:37.00]我是一只小小小小鸟 想要飞呀飞却飞也飞不高
[01:51.00]我寻寻觅觅寻寻觅觅一个温暖的怀抱
[01:56.00]这样的要求算不算太高
[02:03.43]所有知道我的名字的人啊你们好不好
[02:27.33]所有知道我的名字的人啊你们好不好
[02:33.29]世界是如此的小 我们注定无处可逃
[02:40.20]当我 尽人情冷暖 当你决定为你了的理想燃烧
[02:46.67]生活的压力与生命的尊严哪一个重要
[02:54.36]我是一只小小小小鸟 想要飞呀飞却飞也飞不高
[03:07.10]我寻寻觅觅寻寻觅觅一个温暖的怀抱
[03:13.22]这样的要求算不算太高
[03:21.00]我是一只小小小小鸟 想要飞呀飞却飞也飞不高
[03:33.90]我寻寻觅觅寻寻觅觅一个温暖的怀抱
[03:40.31]这样的要求算不算太高
[03:46.17]所有知道我的名字的人啊你们好不好
[03:47.05]这样的要求算不算太高
[03:58.43]所有知道我的名字的人啊你们好不好


一个一个的修改删除,这多没劲啊...所以...
f=File.open('M0030002007.lrc')
f.each do |line|
    puts line.gsub(/[\[\d:\.\]]/,'')
end  

1
1
分享到:
评论
49 楼 fireflyman 2011-04-18  
> Time.now.strftime("%Y-%m-1")
"2011-04-1"


> Time.now.strftime("%A %B %d or %a %e/%m")
=> "Monday April 11 or Mon 11/04"

48 楼 fireflyman 2011-04-15  
简易字符转换
model ApplicationHelper
 def yes_no(bool)
   if bool == true
     "yes"
   else
     "no"
   end
 end
end
47 楼 fireflyman 2011-04-02  

config\initializers目录下建立一个custom_requires.rb,然后...
例如:G:\Program Files\ImageMagick-6.5.6-Q8
应填入-->(不支持空格)
Paperclip.options[:command_path] = "XX:/PROGRA~1/ImageMagick-6.5.6-Q8" 


详细参考-->
Paperclip提示command is not recognized by the 'identify
http://hot88zh.iteye.com/blog/859238
46 楼 fireflyman 2011-04-02  
下面两个相信是最常用的方法.....
logger.info("aaa--#{properties}-----------")  

<pre name="code" class="Ruby"><% logger.info("------#{@properties[2].inspect}----")%>
45 楼 fireflyman 2011-03-28  
IE9出来了,又要弄个新的CSS hack。

  
   body {
        color: green\9; /* IE8 */
        *color: yellow; /* IE7 */
        -color: orange; /* IE6 */
      }
      body:not(:target) {
        color: red\9; /* IE9 */
      }

<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>css hack by 司徒正美</title> <style type="css/text"> body { color: green\9; /* IE8 */ *color: yellow; /* IE7 */ -color: orange; /* IE6 */ } body:not(:target) { color: red\9; /* IE9 */ } </style> </head> <body> </body> </html> 
44 楼 fireflyman 2011-03-23  
In Rails 2 the only way to add multiple validations to a field is through separate validate statements:
validates_presence_of :title
validates_length_of :title, :maximum => 30


Rails 3 simplifies this process by adding a method called validates which is a “shortcut to all default validators”. Using the validates method your code will look like this:
validates(:title, :presence => true, :length => {:maximum => 30})

43 楼 fireflyman 2011-03-01  
Rails 預設是使用 ActiveRecord 下 query。

所以我們會寫出這種 code

@category.posts.published.limit(10)


不過當 category 與 post 是 many to many 的情況下,就會產生 join 的情況。所以 controller 有這種 query 會很痛。
    Post Load (271.4ms) SELECT `posts`.* FROM `posts` INNER JOIN `post_categories` ON `posts`.id = `post_categories`.post_id WHERE ((aasm_state = ‘published’ and published_at <= '2011-02-26 17:54:19') AND ((`post_categories`.category_id = 1))) ORDER BY published_at DESC LIMIT 10


改進方式:

@category.all_posts.published.limit(10)


在 Category 多寫兩個 method,用兩次 select 換掉 join。

  
 PostCategory Load (3.7ms) SELECT post_id FROM `post_categories` WHERE (`post_categories`.category_id = 1)
    Post Load (14.5ms) SELECT * FROM `posts` WHERE ((aasm_state = ‘published’ and published_at <= '2011-02-26 17:35:29') AND (`posts`.`id` IN (4,9,17,18,19,27,28,34,35,37,45,46,50,59,62,63,68,69,71,72,73,75,77,78,79,81,83,90,92,93,97,98,99...............) ORDER BY published_at DESC LIMIT 10 


速度會快上很多倍。
42 楼 fireflyman 2011-01-26  
快速排序
class Array
  def qsort
    return self if self.length <= 1
    pivot = self.shift
    left, right = [],[]
    self.each { |ele| ele <= pivot ? left << ele : right << ele }
    left.qsort + [pivot] + right.qsort
  end
end

41 楼 fireflyman 2011-01-25  
Don´t confuse the right:

 validates_inclusion_of :published, :in => [true, false]

with the wrong:

 validates_inclusion_of :published, :in => %w(true false)

cause:

 %w(true false) == ["true", "false"]

40 楼 fireflyman 2011-01-25  
 [ "w", "x", "y", "z" ][-1]  
#=> "z"

39 楼 fireflyman 2011-01-25  
  "abc.,cde.,efg.,ghi".split(/.(,)/)
  => ["abc", ",", "cde", ",", "efg", ",", "ghi"]
  "abc.,cde.,efg.,ghi".split(/(.)(,)/)
  => ["abc", ".", ",", "cde", ".", ",", "efg", ".", ",", "ghi"]
  "abc.,cde.,efg.,ghi".split(/(.(,))/)
  => ["abc", ".,", ",", "cde", ".,", ",", "efg", ".,", ",", "ghi"]
  "abc.,cde.,efg.,ghi".split(/(.(,))/, 2)
  => ["abc", ".,", ",", "cde.,efg.,ghi"]
  "abc.,cde.,efg.,ghi".split(/(.(,))/, 3)
  => ["abc", ".,", ",", "cde", ".,", ",", "efg.,ghi"]

38 楼 fireflyman 2011-01-25  
in the FirmsController

@firm.people.update(params[:people].keys,params.values)

in the View

<% form_for(@firm) do |f| %>

  <%= f.error_messages %>
  <%= f.text_field :name %>
  <% @firm.people.each do |person| %>
  <% fields_for "people[]", person do |pf| %>
        <%= pf.text_field :name %>
  <% end %>
  <%= f.submit "Save" %>

<% end %> 
37 楼 fireflyman 2011-01-25  
检查数字是否是素数?
def prime?
      ('1' * self) !~ /^1?$|^(11+?)\1+$/
end
irb(main):004:0>   10.prime?
=> false
irb(main):005:0> 11.prime?
=> true


36 楼 fireflyman 2011-01-25  
数组转散列
  array = [['A', 'a'], ['B', 'b'], ['C', 'c']]

  hash = array.inject({}) do |memo, values|
    memo[values.first] = values.last
    memo
  end

  hash
  # => {'A' => 'a', 'B' => 'b', 'C' => 'c'}

35 楼 fireflyman 2011-01-25  
 date_select("user", "birthday", :start_year => 1940, :end_year => Date.current.year - 13)

34 楼 fireflyman 2011-01-25  
 <% remote_form_for "comment",:update =>  "form" do |f| %>
   # your form here
 <% end %>


 <% remote_form_for "comment", :update => {:success => "form", :failure => "errors"} do |f| %>
   # your form here
 <% end %>
33 楼 fireflyman 2011-01-25  
Following the similar egzample by autonomous, here's a simpler version when you just need to write a flexible helper method that takes a block.

For example, suppose you have a method that renders a tree:

  def render_tree(ary, &block)
    concat("<ul>", block.binding)
    for elem in ary
      concat("<li>", block.binding)
      yield elem
      concat("</li>", block.binding)
    end
    concat("</ul>", block.binding)
  end


You can use it in your view, eg:

  <% render_tree(@objects) do |elem| -%>
    <%= elem.title -%>
    <%= link_to 'delete', elem -%>
  <% end -%>


that would return for egzample:

  <ul>
    <li>
      Test title
      <a href="delete">/elems/1</a>
    </li>
  </ul>


Testing concat

To test such helper methods, use the following pattern (a utility method added to your Rspec/unit test suite:
  def render_for(root, options = {})
    _erbout = ''
    render_tree(root, options) do |node|
      _erbout.concat(node.title)
    end
    _erbout
  end


and test like this (RSpec example):

  it "should return abc" do
    render_for(object).should == 'abc'
  end

32 楼 fireflyman 2011-01-24  
validates_format_of :uri, :with => URI.regexp(['http'])

31 楼 fireflyman 2011-01-23  
Link to Unimplemented

# public/javascripts/application.js
function unimplemented() {
  alert("NOTICE\n\nThis feature is not implemented yet. Please check back again soon!");
}

This allows us to do the following:


  <a href="javascript:unimplemented();">link text</a>


# app/helpers/application_helper.rb
def link_to_unimplemented( link_text, *args )
  link_to_function( link_text, 'unimplemented()', *args)
end

Now, we’re able to use link_to_unimplemented and pass any arguments that you’d pass to the default link_to view helper.


<%= link_to_unimplemented( 'link text', { :class => 'link_class_name' } ) -%>

30 楼 fireflyman 2011-01-23  

  <div id="footer">
    &copy; Copyright <%= current_year -%>. All Rights Reserved.
  </div>


  # add to application_helper.rb
  module ApplicationHelper
    def current_year
      Time.now.strftime('%Y')
    end
  end

相关推荐

Global site tag (gtag.js) - Google Analytics