accessor in Ruby?

I am having a hard time understanding attr_accessor in Ruby. Can someone explain this to me? Let's say you have a class Person . class Person end person = Person.new person.name # => no method error Obviously we never defined method name . Let's do that. class Person def name @name # simply returning an instance variable @name end end person = Person.new person.name #

Ruby中的访问器?

我很难理解Ruby中的attr_accessor 。 谁可以给我解释一下这个? 假设你有一个Person类。 class Person end person = Person.new person.name # => no method error 显然我们从来没有定义方法name 。 让我们这样做。 class Person def name @name # simply returning an instance variable @name end end person = Person.new person.name # => nil person.name = "Dennis" # => no method error 阿哈

Check if a value exists in an array in Ruby

I have a value 'Dog' and an array ['Cat', 'Dog', 'Bird'] . How do I check if it exists in the array without looping through it? Is there a simple way of checking if the value exists, nothing more? You're looking for include? : >> ['Cat', 'Dog', 'Bird'].include? 'Dog' => true There is an in? method in ActiveSupport (part of Rails) since v3.1, a

检查Ruby中数组是否存在值

我有一个价值'Dog'和数组['Cat', 'Dog', 'Bird'] 。 如何检查它是否存在于数组中而不循环? 有没有简单的方法来检查值是否存在,仅此而已? 你正在寻找include? : >> ['Cat', 'Dog', 'Bird'].include? 'Dog' => true 有一个in? 方法在ActiveSupport (Rails的一部分)自v3.1以来,正如@campaterson所指出的那样。 所以在Rails中,或者如果你require 'active_support

How to write a switch statement in Ruby?

我如何在Ruby中编写switch语句? Ruby uses the case expression instead. case x when 1..5 "It's between 1 and 5" when 6 "It's 6" when "foo", "bar" "It's either foo or bar" when String "You passed a string" else "You gave me #{x} -- I have no idea what to do with that." end Ruby compares the object in the when clause with the object in the case clause using the === operator. For example,

如何在Ruby中编写switch语句?

我如何在Ruby中编写switch语句? Ruby使用case表达式。 case x when 1..5 "It's between 1 and 5" when 6 "It's 6" when "foo", "bar" "It's either foo or bar" when String "You passed a string" else "You gave me #{x} -- I have no idea what to do with that." end Ruby使用===运算符将when子句中的对象与case子句中的对象进行比较。 例如, 1..5 === x ,而不是x === 1..5 。 这使得复杂的when从句由上述

Calling shell commands from Ruby

How do I call shell commands from inside of a Ruby program? How do I then get output from these commands back into Ruby? This explanation is based on a commented Ruby script from a friend of mine. If you want to improve the script, feel free to update it at the link. First, note that when Ruby calls out to a shell, it typically calls /bin/sh , not Bash. Some Bash syntax is not supported by

从Ruby调用shell命令

如何从Ruby程序内部调用shell命令? 那我如何从这些命令的输出回到Ruby? 这个解释基于来自我的一位朋友的评论Ruby脚本。 如果您想改进脚本,请随时在链接中更新它。 首先,请注意,当Ruby调出一个shell时,它通常会调用/bin/sh而不是Bash。 在所有系统上,某些Bash语法不受/bin/sh支持。 以下是执行shell脚本的方法: cmd = "echo 'hi'" # Sample string that can be used Kernel#` ,通常称为反引号 - `cmd` 这与

Why and when to use shell instead of Ruby

Since I am conversant in Ruby, I am about to script a few things on OSX using it. But then I thought, perhaps I am missing the boat. I know a lot of reasons to prefer Ruby over Bash (or whatever sh-compatible command language interpreter), but I don't know any reasons not to. What is the upside of programming the shell directly? I intend to take advantage of system commands using system

为什么以及何时使用shell而不是Ruby

由于我熟悉Ruby,因此我将在OSX上使用它编写一些脚本。 但后来我想,也许我错过了这条船。 我知道很多理由更喜欢Ruby而不是Bash(或任何sh兼容的命令语言解释器),但我不知道没有任何理由。 直接编写shell的好处是什么? 我打算在必要时利用系统使用system命令。 注意:我已经知道Ruby不会一直存在,但我主要对技术,语义和语法标准感兴趣。 编辑:由Ruby不总是在那里,我的意思是它不是所有* nix发行版的标准部分,不