Ruby China
  • 社区
  • 招聘
  • Wiki
  • 酷站
  • Gems
  • 注册
  • 登录
@piecehealth
会员
第 6826 位会员 / 2013-04-23

[email protected]
GOAT
上海
5 篇帖子 / 286 条回帖
14 关注者
6 正在关注
40 收藏
未设置 GitHub 信息。
  • 概况
  • 话题
  • 回帖
  • 收藏
  • 正在关注
  • 关注者
  • 在 url 中传入关键值,而后在 controller 中对 params 进行判断按需返回数据,这种做法好吗? at 2014年10月08日

    如果 status 是 enum 类型,controller 里@posts = Post.send params[:status]

  • 怎么终止程序运行(不往下执行代码)单并不退出程序? at 2014年10月08日

    #10 楼 @rubysir ruby 在方法里 return 也可以啊,你没有试过么。

  • 怎么终止程序运行(不往下执行代码)单并不退出程序? at 2014年10月08日

    #8 楼 @rubysir 你想用的应该是exit吧,你的意思应该是c#里return的是mian方法吗? c#写这段代码

    class Program
    {
        static void Main(string[] args)
        {
            if(1 < 2)
                Console.WriteLine("OK");
            else {
                Console.WriteLine("No");
                return;
            }
            Console.WriteLine("阿弥陀佛");
        }
    }
    

    不光达不到你要效果,还会有 else 分支永远执行不到的 warning。

  • 怎么终止程序运行(不往下执行代码)单并不退出程序? at 2014年10月08日

    #5 楼 @rubysir 把c#代码贴出来看看怎么简洁易懂。

  • 怎么终止程序运行(不往下执行代码)单并不退出程序? at 2014年10月08日

    用gets吧

  • 自定义一个 Validate 里使用 self::count 为何提示没有方法呢? at 2014年10月06日

    方法里的 self 是 DefaultWarningConfig 的实例,不是 DefaultWarningConfig 本身。def 会打开一个新的作用域,def 里面的 self 跟外面的 self 不是一个东西。

    self.class是DefaultWarningConfig说明 self 是 DefaultWarningConfig 的实例。

    class DefaultWarningConfig
    
      COUNT = 10
    
      p self # DefaultWarningConfig 本身
    
      def instance_method
        p self # DefaultWarningConfig的一个实例
        P self.class # DefaultWarningConifg本身
        p self.class::COUNT # 10
        p DefaultWarningConfig::COUNT # 10
        p self::COUNT # Error
      end
    
    end
    
  • [淄博] 农夫通招聘 rails,真诚寻找合作伙伴。 at 2014年09月21日

    张店人帮顶

  • do ... end 和 {} 不是通用的么? at 2014年08月29日

    define_method(k.to_sym) { v }加个括号就好了。

  • [周末杂谈] Ruby 元编程之 E=mc2 at 2014年08月24日

    E = mc2 = Class.singleton_class.singleton_class ……不知道有什么用,求指导

  • 如何在多线程中获取每个线程的返回值 at 2014年07月11日

    这样可以么?

    thr = Thread.new { Thread.current[:return] = "some hash" }
    thr.join
    thr[:return] # some hash
    
  • bundle install 直接报错 at 2014年06月25日

    JRuby 不支持 C extension 的吧

  • 如何通过类名获得类的命名空间? at 2014年06月18日

    #7 楼 @jky klass.inspect.split('::').include? 'B'

  • 如何通过类名获得类的命名空间? at 2014年06月18日

    #4 楼 @jky 不知道我只得到B是什么意思,如果 B 是 class 的话,那B.inspect 就可以了,如果 B 是字符串"B"的话,可以ObjectSpace.each_object(Class) {|klass| p klass if klass.inspect.include? 'B'}

  • 过来写个乘法口诀 at 2014年06月13日

    https://ruby-china.org/topics/16483

  • 求两个数组之间转换的写法。 at 2014年06月06日

    #3 楼 @chenge 写复杂了……

    arr.each_with_index do |a, i|
        a.each do |str|
            str =~ /^(.*):(.*)$/
            k, v = $1, $2
            hash[k] ||= [nil] * arr_size
            hash[k][i] = v
        end
    end
    

    就行

  • 求两个数组之间转换的写法。 at 2014年06月06日
    arr = [["a:1","b:0"],["c:2","b:0","d:5"]]
    
    hash = {}
    arr_size = arr.size
    
    arr.each_with_index do |a, i|
        a.each_with_object({}) do |str, h|
            str =~ /^(.*):(.*)$/
            h[$1] = $2
        end.each do |k, v|
            hash[k] ||= [nil] * arr_size
            hash[k][i] = v
        end
    end
    
    p hash
    
  • 赋值问题 at 2014年05月28日

    你的需求应该subWorkFlowPath = doc.elements.map("library/section") { |e| e.attributes["name"] }

  • 赋值问题 at 2014年05月28日

    ruby所有的方法都有返回值,Array#each返回数组本身,即puts subWorkFlowPath跟puts doc.elements是一样的

  • 用 attr_reader 添加所有实例变量的方法? at 2014年05月27日

    用 method_missing 也可以

  • 时间的遍历如何写? at 2014年05月21日

    #4 楼 @QueXuQ 你看 Date 的方法啊,有 next_month, next_year,而且遍历天的时候顺便年月不都遍历了么。

  • 时间的遍历如何写? at 2014年05月20日

    有

    require 'date'
    
    day1 = Date.new(2013, 10, 9)
    day2 = Date.new(2014, 5, 20)
    
    (day1..day2).each do |i| 
    # do something
    end
    
  • ruby 的 extend 方法为什么在类和实例上实现的逻辑不一致 at 2014年05月19日

    我认为一个对象的单例方法优先级最高,上面的第二段代码跟我下面的代码结果一致。

    module Foo
      def bar
         "bar in the Module Foo"
      end
    end
    
    class FooBar
    end
    
    f = FooBar.new
    
    class << f
        def bar
            "bar in class FooBar"
        end
    end
    
    f.extend Foo
    f.bar
    
  • 在 module 自定义 method_missing 方法 at 2014年05月11日

    主要是 Ruby 的钩子方法,让父类(或 module)对子类(或 include module 的对象)的控制能力异常的强大。

  • 来段小清醒的代码 at 2014年05月07日
    def huiwen str
        0.upto(str.size / 2) {|i| return false if str[i] != str[-(i + 1)]}
        true
    end
    
  • 凑数 at 2014年05月06日

    之前写过凑 24 点的,可以借鉴一下 https://gist.github.com/piecehealth/9342052

  • 求数组的子集 at 2014年05月04日

    #2 楼 @bluexuemei

    a = [*0..9]
    
    def c arr, n
        return arr.map {|i| [i]} if n == 1
        rets = []
        arr.each_with_index do |ele, i|
            rets += c(arr[i + 1, arr.size - i - 1], n - 1).map {|a| [ele, *a]}
        end
        rets
    end
    
    def all_sub_arrs arr
        rets = []
        1.upto(arr.size) do |i|
            rets += c(arr, i)
        end
        rets
    end
    
    require 'benchmark'
    
    Benchmark.bm do |x|
        x.report do
            result = []
            1.upto(a.size) {|i| a.combination(i).each {|sub_arr|  result << sub_arr.to_a}}
        end
        x.report {result = all_sub_arrs a}
    end
    

    自己实现 combination 肯定没有 ruby 自带的快,Array 内置的方法都是 c 写的。

  • 求数组的子集 at 2014年05月04日

    1.upto(a.size) {|i| a.combination(i).map {|sub_arr| puts sub_arr.to_a.join(', ')}}

  • 数字分组 1 at 2014年04月30日
    arr = (0..9).to_a.shuffle
    [arr[0, 2], arr[2, 4], arr[6, 4]].map &:sort
    

    两行不行么

    没看见所有

  • 有多少童鞋看过 Rails Tutorial 之后还无从下手的? at 2014年04月10日

    人家开头就说了,Rails Tutorial 不是面向纯新手的

    Inexperienced programmers: The Rails Tutorial is not aimed principally at beginning programmers, and web applications, even relatively simple ones, are by their nature fairly complex.

    不过对我(非职业 web 开发人员)来说,Rails Guides 更合适一些。

  • 上海书店竟然没有一本标题有 Ruby 的书 at 2014年03月26日

    我的《Ruby 元编程》就是在上海书城买的……陪别人逛的时候偶尔看到,马上就买了。

  • 上一页
  • 1
  • 2
  • …
  • 6
  • 7
  • 8
  • 9
  • 10
  • 下一页
关于 / RubyConf / Ruby 镜像 / RubyGems 镜像 / 活跃会员 / 组织 / API / 贡献者
由众多爱好者共同维护的 Ruby 中文社区,本站使用 Homeland 构建,并采用 Docker 部署。
服务器由 赞助 CDN 由 赞助
iOS 客户端 / Android 客户端 简体中文 / English