Questions
180318
- [ ] search for the meaning of symbols(& : )
Learn Ruby The Hard Way
170928
- [ ] to_proc
[ ] ##### Proc
Proc
objects are blocks of code that have been bound to a set of local variables. Once bound, the code may be called in different contexts and still access those variables.def gen_times(factor) return Proc.new {|n| n*factor} end times3 = gen_times(3) times3.call(12) #=> 36
[ ] .map
[ ] .next
[ ] 正則表達式 /^/ = ?
- [ ] Integer?
[x] next
Next https://nodejust.com/ruby-program-tutorials-loops-iterators/關鍵字是用來跳過某一輪的循環。它可以用任何的循環裡,不論是While, Until, For 還是其他的iterators。
for i in 1..5
next if i % 2 == 0
print i
end
# even number will not be printed.
- [ ] break
- [ ] =~
- [ ] class 類別內定義的function名稱何時需要用self.做開頭?
170909
Test::Unit 參閱
170701
Exercise 46
open(WORD_URL) {|f|
f.each_line {|word| WORDS.push(word.chomp)}
}
include
=
::
=class TestNAME < Test::Unit::TestCase
The::
is the scope resolution operator. What it does is determines what scope a module can be found under.
module A
def self.method; "Outer"; end
end
module B
module A
def self.method; "Inner"; end
end
A.method # => "Inner"
::A.method # => "Outer"
end
.map
=
170620
Exercise 43 - Study Drills:
書要我們去看一下這個東西 "finite state machine"
170606
Exercise 43
找出讓輸入條件變得簡單的方法:
if action == "shoot"
如何能接受其他輸入?
"finite state machine"
類別變數 @@val
http://blog.annideas.com/ironman/2014-10-21-ruby-girl-21-ruby-instance-and-class-variables/
正則表達式:
表格
[] |
指定的範圍(例如:[a-z] 表示一個在a 到z 的範圍內的字母) |
---|---|
\w |
一般字元 (word character),即[0-9A-Za-z_] |
\W |
非一般字元 (non-word character) |
\s |
空白字元 (space character),即[ \t\n\r\f] |
\S |
非空白字元 (non-space character) |
\d |
數字 (digit character),即[0-9] |
\D |
非數字 (non-digit character) |
\b |
退位 (0x08)(僅用於指定的範圍) |
\b |
單字邊界(若不是於指定的範圍) |
\B |
非單字邊界 |
* |
前一符號的內容出現 0 或數次。 |
+ |
前一符號的內容出現 1 或數次。 |
{m,n} |
前一符號的內容,最少出現 m 次,最多出現 n 次。 |
? |
前一符號的內容最多出現一次,同{0,1} |
` | 符合前一個或後一個表示式 |
() |
分組 |
「替换」
gsub
很多时候匹配是为了替换,Ruby中进行正则替换非常简单,两个方法即可搞定,sub()+gsub()。
sub只替换第一次匹配,gsub(g:global)会替换所有的匹配,没有匹配到返回原字符串的copy
str = "ABDADA"
new_str = str.sub(/A/, "*") #返回"*BDADA"
new_str2 = str.gsub(/A/, "*") #返回"*BD*D*"
170408- about for loop in Ruby
.each_with_index
在 stackoverflow.com 看到的新招,待研究:['foo', 'bar', 'baz'].each_with_index {|j, i| puts "#{i} #{j}"}
Date
錯誤?start = Date.new(2013, 06, 30) stop = Date.new(2011, 06, 30) # step back in time over two years, one week at a time start.step(stop, -7).each do |d| puts d end
170403-
"%Q, %q, %W, %w, %x, %r, %s"用法
ex24:
最後一行出現的%s
%d
的用法還沒完全搞懂。
看來是將後面的 function 值叫出來並轉譯。%d
Convert argument as a decimal number.%s
Argument is a string to be substituted. If the format sequence contains a precision, at most that many characters will be copied.
170328-
What are symbols below do for?&&
and?
#yes, && = and
170129 -
echo "This is a test." > test.txt
170127 -
Ruby File.open mode
開啟檔案的模式為何分這麼多種?
Why exercise16 use target = open(filename, 'w')
in write
mode.
170104 -
What is the difference between ''' and """ when using format string?
161207 -
Floating number?
14253456
There were some weird things happened when I trying to calculate floating number with irb
:
$ 3.785 - 2
This should be a very easy calculation, but the answer shows:
=> 1.7850000000000001
WHY? -> 170923 -
在「為你自己學 RUBY」找到說明如下:
http://railsbook.tw/chapters/06-ruby-basic-2.html
浮點數是不太準確的
浮點數就是帶有小數點的數字,讓我們來看一段簡單的運算式:
puts 4.51212 == (3.51212 + 1)
我們光用肉眼就看得出來這應該是相等的,但執行之後會發現結果是 false,表示這兩個值不相等。這是因為浮點數本身就是沒辦法非常精準,如果真的需要精準的計算,可使用 Ruby 的標準函式庫BigDecimal做個轉換:
require 'bigdecimal'
puts BigDecimal("4.51212") == BigDecimal("3.51212") + BigDecimal("1") # => true