You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert
My incorrect assumption that the Ruby and Python versions would behave the same ultimately caused a bug in production code. When investigating the bug I was surprised to find that there was a difference between the two languages and it wasn’t clear to me why Python and Ruby had chosen to do different things. I decided to investigate. Why does Python do what it does? This is easy to answer as the c
I've been using Ruby for over ten years, and I'm still surprised to find new corners to explore. It wasn't until I came across this book that I realized how deep the string "rabbit hole" went. Why Strings? They're everywhere! As devs, you and I create tons of strings. Probably more than any other object on a day to day basis. How much do you really know about strings? How about encodings? How fami
Could we drop Symbols from Ruby? Don’t know about you, but I personally have been hit a least a dozen times by bugs caused by strings vs symbols distinction. That happened in my own code, and it happened when using some other libraries as well. I like how symbols look in the code, but I don’t like the specific distinction that is made between them and strings. In my (perhaps controversial opinion)
I love the Ruby language, undoubtedly! When you love something, you should love not only the bright side but also the dark side of it too, unconditionally. Probably that is the message Katy Perry tries to deliver. In this post I am trying to point out some Ruby features you might want to use with a lot of caution. I have another post about five gotchas in Rails because I love the framework too. 1.
chomp(rs = $/) -> String[permalink][rdoc][edit] self の末尾から rs で指定する改行コードを取り除いた文字列を生成して返します。ただし、rs が "\n" ($/ のデフォルト値) のときは、実行環境によらず "\r", "\r\n", "\n" のすべてを改行コードとみなして取り除きます。 rs に nil を指定した場合、このメソッドは何もしません。 rs に空文字列 ("") を指定した場合は「パラグラフモード」になり、実行環境によらず末尾の連続する改行コード("\r\n", "\n")をすべて取り除きます。 例 p "foo\n".chomp # => "foo" p "foo\n".chomp("\n") # => "foo" p "foo\r\n".chomp("\r\n") # => "foo" $/ = "\n" # デ
Ruby 1.9 から文字列や正規表現オブジェクトはそれぞれエンコーディング(いわゆる文字コード)を保持するようになりました。 たとえば 0xB1 0xB2 という2バイトは EUC-JP エンコーディングでは「渦」、SHIFT_JIS エンコーディングでは「アイ」という文字になります。つまり同じバイト列でもエンコーディングが異なれば異なる文字として解釈されます。 1.8 では文字列はただのバイト列でした。なので、それがどのような文字を表しているのか、つまりエンコーディングが何なのかはプログラムが知っている必要がありました。 1.9 では文字列オブジェクト自身が自分が何のエンコーディングかを知っています。同じ 0xB1 0xB2 というバイト列でも、それが EUC-JP の「渦」なのか SHIFT_JIS の「アイ」なのかは、文字列自身が知っています。 スクリプトエンコーディング スクリプ
プログラミングにおいて、文字列補間(もじれつほかん、string interpolation)とは、文字列リテラル内に埋め込まれたプレースホルダーを実行時に評価し、そのプレースホルダーを対応する値に置き換える処理である。変数補間 (へんすうほかん、variable interpolation)、変数置換(へんすうちかん、variable substitution)、変数展開(へんすうてんかい、variable expansion)ともいう。この処理は、単純なテンプレートエンジンであり[1]、正式な用語で言えば準引用(英語版)の一形態である。文字列補間は、文字列連結よりも簡単でより直観的に文字列のフォーマットを規定できる[2]。 文字列補間は、データの文字列表現を多用する多くのプログラミング言語(C言語、Perl、PHP、Python、Ruby、Groovy、Scala、Swiftなど、およ
hex -> Integer[permalink][rdoc][edit] 文字列に 16 進数で数値が表現されていると解釈して整数に変換します。接頭辞 "0x", "0X" とアンダースコアは無視されます。文字列が [_0-9a-fA-F] 以外の文字を含むときはその文字以降を無視します。 self が空文字列のときは 0 を返します。 例 p "10".hex # => 16 p "ff".hex # => 255 p "0x10".hex # => 16 p "-0x10".hex # => -16 p "xyz".hex # => 0 p "10z".hex # => 16 p "1_0".hex # => 16 p "".hex # => 0 [SEE_ALSO] String#oct, String#to_i, String#to_f, Kernel.#Integer, Ker
I recently published an article in which I tested most of Ruby's string methods with certain Unicode characters to see if they would behave unexpectedly. Many of them did. One criticism that a few people had of the article was that I was using unnormalized strings for testing. Frankly, I was a bit fuzzy on Unicode normalization. I suspect that many Rubyists are. Using normalization, you can take m
We could also call to_s directly on the class and get a valid result as well since the Class class is also an object: However if we try to do the same with to_str, it won’t work because it’s not defined on a higher level class: puts Demo.new.to_str This will return undefined method to_str for #<Demo:0x007fea8204e290> (NoMethodError), meaning that we have to define it ourselves when creating a new
On upcoming immutable string literals in Ruby … and check why 5600+ Rails engineers read also this On upcoming immutable string literals in Ruby Today I checked one of the solutions made by our Junior Rails Developer class student. As part of the course they make Rails apps but also learn from smaller code examples delivered by exercism.io. I found there an opportunity for him to learn more about
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く