ruby/rails block content problem

i wont to create simple tabs helper for dynamic tabs creation. Something like form_for rails helper. Here what i have for now (simplified for example ): class Tabs attr_reader :tabs, :type, :subtype, :args def initialize(*args) @tabs = [] @type = args[0] || 'horizontal' @subtype = args[1] || 'basic' @args = args.extract_options! end def ta

ruby / rails阻止内容问题

我不会创建简单的选项卡帮助动态选项卡创建。 类似于form_for rails helper。 在这里,我现在有什么(例如简化): class Tabs attr_reader :tabs, :type, :subtype, :args def initialize(*args) @tabs = [] @type = args[0] || 'horizontal' @subtype = args[1] || 'basic' @args = args.extract_options! end def tab(*args, &block) tab ={}

Iterate through every file in one directory

How do I write a loop in ruby so that I can execute a block of code on each file? I'm new to ruby, and I've concluded that the way to do this is a do each loop. The ruby file will be executed from a different directory than the directory I want to loop through. I've tried the Dir.foreach and I couldn't get it to work. As others have said, Dir.foreach is a good option here.

迭代一个目录中的每个文件

如何在ruby中编写循环以便我可以在每个文件上执行一段代码? 我对ruby很陌生,我已经断定要做到这一点的方法是每个循环。 ruby文件将从不同于我要循环访问的目录的目录执行。 我已经尝试过Dir.foreach ,但无法启动它。 正如其他人所说, Dir.foreach在这里是一个不错的选择。 但是请注意, Dir.entries和Dir.foreach将始终显示. 和.. (当前和父目录)。 你通常不想在他们身上工作,所以你可以做这样的事情: Dir.fo

Ruby: Proc#call vs yield

What are the behavioural differences between the following two implementations in Ruby of the thrice method? module WithYield def self.thrice 3.times { yield } # yield to the implicit block argument end end module WithProcCall def self.thrice(&block) # & converts implicit block to an explicit, named Proc 3.times { block.call } # invoke Proc#call end end WithYield:

Ruby:Proc#调用vs yield

什么是以下两种实现方式之间Ruby中的行为差异thrice方法? module WithYield def self.thrice 3.times { yield } # yield to the implicit block argument end end module WithProcCall def self.thrice(&block) # & converts implicit block to an explicit, named Proc 3.times { block.call } # invoke Proc#call end end WithYield::thrice { puts "Hello world" } WithProcCall::thrice {

Ruby's yield feature in relation to computer science

I recently discovered Ruby's blocks and yielding features, and I was wondering: where does this fit in terms of computer science theory? Is it a functional programming technique, or something more specific? Ruby's yield is not an iterator like in C# and Python. yield itself is actually a really simple concept once you understand how blocks work in Ruby. Yes, blocks are a functional

Ruby与计算机科学相关的良率特征

我最近发现了Ruby的块和屈服特征,我想知道:这在计算机科学理论方面适合什么? 它是一种函数式编程技术,还是更具体的东西? Ruby的yield不像C#和Python中的迭代器。 一旦你理解了Ruby中的块如何工作, yield本身实际上是一个非常简单的概念。 是的,块是一种功能性编程功能,即使Ruby不是一种功能正常的语言。 事实上,Ruby使用lambda方法创建块对象,这是从Lisp的语法中借用来创建匿名函数的 - 这就是块的功能。 从

Ruby: How to check whether a string isn't blank?

In my app I want to prevent a variable from being blank — eg, a user's name should be something other than spaces and other whitespace. It will work like this: foobar = gets.strip arr = [] if # the string in the variable 'foobar' isn't blank arr.push(foobar) end Rails adds blank? method to class String , so that "".blank? , " ".blank? , and nil.blank? are all tr

Ruby:如何检查一个字符串是否不为空?

在我的应用程序中,我想防止变量变为空白 - 例如,用户的名称应该是空格和其他空格以外的内容。 它会像这样工作: foobar = gets.strip arr = [] if # the string in the variable 'foobar' isn't blank arr.push(foobar) end Rails添加blank? 方法来类String ,这样"".blank? , " ".blank? 和nil.blank? 都是true 。 Ruby有类似的empty? 方法,但它不同于blank? ,正如我们在下面的例子中可

Duplicating .blank? in standard Ruby

Rails has a .blank? method that will return true if an Object is empty? or nil?. The actual code for this can be found here. When I try on 1.9.2 to duplicate this by doing: class Object def blank? respond_to?(:empty?) ? empty? : !self end end Calling "".blank? returns true but calling " ".blank? returns false when according to the rails documentation a whites

复制.blank? 在标准的Ruby中

Rails有一个.blank? 方法,如果一个Object是空的,它将返回true? 或无? 实际的代码可以在这里找到。 当我尝试在1.9.2上重复这个操作时: class Object def blank? respond_to?(:empty?) ? empty? : !self end end 调用“”.blank? 返回true,但调用“”.blank? 根据rails文档,空白字符串应该为.blank评估为true时返回false? 在我查阅我最初写的代码之前: class Object def blank? !!self.empty? |

What's the nature of "property" in a Ruby class?

I don't understand the keywords like attr_reader or property in the following example: class Voiture attr_reader :name attr_writer :name property :id, Serial property :name, String property :completed_at, DateTime end How do they work? How can I create my own? Are they functions, methods? class MyClass mymagickstuff :hello end That are just class methods. In this exampl

Ruby类中“属性”的性质是什么?

在下面的例子中,我不理解像attr_reader或property这样的关键字: class Voiture attr_reader :name attr_writer :name property :id, Serial property :name, String property :completed_at, DateTime end 他们如何工作? 我如何创建自己的? 它们是功能还是方法? class MyClass mymagickstuff :hello end 这只是类方法。 在这个例子中, has_foo向放置字符串的实例添加一个foo方法: module Foo de

Ruby constructor self vs @

I have created folowing simple class: class Test def initialize(a, b) @a = a @b = b end def test puts @a end end IS there a way to replace @a with self ? Everytime I tried to do this I received an error: undefined method `a' The reason I am doing this is because I would like to create a new object with two parameters, and later operate on these parameters like: d = MyOb

Ruby构造函数vs vs @

我创建了以下简单的类: class Test def initialize(a, b) @a = a @b = b end def test puts @a end end 有没有办法用self替换@a ? 每次我试图做到这一点,我收到一个错误: undefined method `a' 我这样做的原因是因为我想创建一个带有两个参数的新对象,然后对这些参数进行操作,如: d = MyObject('title', 'Author') d.showAuthor 它可以完成,实际上是为这些类完成的:Array,String,Integer

Why are setter methods not used in initialization?

Recently I've been reading "Practical Object Oriented Design in Ruby", and I noticed one of the best practices was to use accessor methods instead of directly grabbing the @instance_variable . For example: class Foo attr_accessor :bar def initialize(my_argument) @bar = my_argument end # bad # def lorem_ipsum # @bar * 999 # end # good def lorem_ipsum

为什么在初始化时不使用setter方法?

最近我一直在阅读“面向实践的面向对象的Ruby设计”,并且我注意到其中一个最佳实践是使用访问器方法,而不是直接抓取@instance_variable 。 例如: class Foo attr_accessor :bar def initialize(my_argument) @bar = my_argument end # bad # def lorem_ipsum # @bar * 999 # end # good def lorem_ipsum bar * 999 end end 保持DRY是有意义的,并且在需要在实际获取其值的情况下以某种

rails 3.1, why devise can't build the resource?

I've noticed that created resouce attributes are all blank after executing build_resource at Devise::RegistrationsController create function here is the params object sent: "student"=>{"first_name"=>"George", "last_name"=>"Michle", "company_name"=>"Meri", "work_title"=>"Architect", "address"=>"Moon", "postal_code"=>"23410", "work_phone"=>"", "home_phone"=>

rails 3.1,为什么设计不能建立资源?

我注意到,在Devise::RegistrationsController创建函数中执行build_resource之后,创建的资源属性都是空白的 这里是发送的params对象: "student"=>{"first_name"=>"George", "last_name"=>"Michle", "company_name"=>"Meri", "work_title"=>"Architect", "address"=>"Moon", "postal_code"=>"23410", "work_phone"=>"", "home_phone"=>"", "mobile_phone"=>"", "fax"=>"", "emai