かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[Ruby再入門]演算子の優先順位

今度は演算子について。
演算子は、+だとか-だとか*だとか色々あるけれど、全部を列挙するとこれくらいになる。演算子の優先度は表の上にあるほうが高くなる。

演算子 意味
:: スコープ
ModuleName::ClassNameのように使う
[] インデックス
array[0]のように使う
** 累乗
2 ** 3 # 8になる
+, -, !, ~ 単項演算子
*, /, % 乗算,割り算,剰余
+, - 足し算, 引き算
<<, >> 論理シフトとか
& ビットごとのAND
|, ^ ビットごとのOR, XOR
>, >=, <, <= 比較
==, ===, <=>, !=, =~, !~ 等価, 不等など
&& 論理AND
|| 論理OR
.., ... 範囲演算子
= (含む+=, -=, *= ...) 代入演算子
A ? B : C 三項演算子
not 論理否定
and, or 論理AND, OR

 

今までJavaやC++とかC#をやってきて見慣れないものもいくつかあるものの、大体見覚えのある演算子が並んでいる。
Ruby以外ではあまり見たこと無い演算子として、===, <=>がある。

===はcase文での比較のときにつかわれる演算子らしい。==ではないんだね。これによって正規表現と文字列の比較とかもcase文でうまいこといけるようになってるみたいだ。
<=>演算子は、左辺と右辺を比較して、等しかったら0, 左辺が小さかったら-1, 左辺が大きかったら1を返す演算子のようだ。

puts 1 <=> 2 # -1
puts 2 <=> 1 # 1
puts 1 <=> 1 # 0

ほかに珍しいものとしては、and or notなどがある。論理ANDとかという意味では珍しくないけど、andとorの優先順位が同じだったり、代入演算子と比べて優先度が低かったりと特徴的だ。
ここらへんは、どういったことがおきるのかTHREE=ONEさんのブログの以下の記事で書かれてる。
Ruby の論理演算子

演算子だけをとってみても奥が深そうだ。

因みに、演算子のうちのいくつかはメソッドとして呼び出せるみたいだ。
例えば+とかは、変数.+(引数)といった形で書いても動く。

NetBeans6.5のRubyエディタでは、演算子をメソッドとして呼び出すような補間はCtrl+Spaceをしても出てこなかった。

コード補間のリストには+とか-は出てこなかった
image 

まぁ実質問題はない!

投稿日時 : 2008年12月6日 18:56

Feedback

# longchamp outlet 2012/12/14 20:41 http://www.longchampbagoutlet.info/category/longch

Think it as being a journal or perhaps a magazine for general interest proper to study. Funny, witty, controversial, entertaining, useful, acerbic, thoughtful, serious, curious, unexpected comebacks really are welcome.

# longchamp bag outlet 2012/12/15 15:45 http://www.longchampbagoutlet.info/category/longch

make these products red using a yellow moose!!

# burberry sale 2012/12/15 22:54 http://www.burberryuksale.info/category/burberry-c

The only folks that would appear good sporting these fugly things can be Ferrari pit crew within the pits:D

# sacs longchamp 2012/12/16 17:53 http://www.saclongchampachete.info/category/longch

Thus, our shelves end up filled with points that we get pleasure from.

# michael kors sac 2012/12/18 2:06 http://sacmichaelkorssoldes.monwebeden.fr/#/bienve

We found a multitude of great DVDs that people were excited to look out again. Over the lifetime of two months.

# sac michael kors pas cher 2012/12/18 20:08 http://sacmichaelkorssoldes.monwebeden.fr

we re-watched our creator of the particular Rings trilogy, the Godfather trilogy, and around twenty different movies that him and i loved as well as hadn¡¯t watched in a very while.

# sac longchamp noir soldes 2012/12/18 20:43 http://www.longchampfr.info/category/sac-longchamp

The stars of that pool are classified as the comments and the pictures are generally secondary.

# burberry bags 2012/12/19 13:38 http://burberryukoutlets.wordpress.com/category/bu

It's OKAY. You can definitely a digg star =)

# michael kors soldes 2012/12/21 4:04 http://sacmichaelkors.monwebeden.fr

If many sound beneficial I'd definitely wear these from home.

# sacs guess 2012/12/22 17:45 http://sacsguess.monwebeden.fr

Keep the excellent function.

# destockchine 2013/01/10 22:33 http://www.destockchinefr.fr/nike-shox-pas-cher/ni

Absolutely love will be the merely sane coupled with reasonable solution attached to worker living.
destockchine http://www.destockchinefr.fr/nike-shox-pas-cher/nike-shox-nz-2-pas-cher/

# destockchine 2013/01/10 22:34 http://www.destockchinefr.fr/veste-marque-pas-cher

Around the world prehaps you are one individual, then again to 1 man prehaps you are the modern world.
destockchine http://www.destockchinefr.fr/veste-marque-pas-cher/veste-ed-hardy-pas-cher/

# chine destock 2013/01/28 23:55 http://www.destockchinefr.fr/maillot/maillot-br/

Don‘g throw away your time more than gentleman/young lady,who actually isn‘g willing to throw away his / her period with you. chine destock http://www.destockchinefr.fr/maillot/maillot-br/

# esGkzDlPjsxDviokeS 2014/08/04 4:24 http://crorkz.com/

fUoz90 Really informative post.Really looking forward to read more. Really Great.

# PLjnTnjahOvx 2014/09/03 19:30 https://www.youtube.com/watch?v=ietNoQGnfas

I'll immediately grasp your rss as I can not in finding your email subscription link or newsletter service. Do you have any? Kindly let me realize in order that I may just subscribe. Thanks.

# HWVphvexifSqF 2014/09/10 7:40 http://www.theboatonlinestore.com/

You are a very intelligent person!

# IsQNrtCpKOqFMfC 2014/09/12 17:58 https://www.youtube.com/watch?v=jtXyUiBeflE

Hello there, You have performed a fantastic job. I will certainly digg it and for my part recommend to my friends. I am confident they will be benefited from this website.

# MvwSAFoGua 2014/09/18 16:04 http://bigdataaah.info/story.php?id=20853

wi3TJM Very good blog post.Much thanks again. Want more.

# oJVgtCRicFQ 2015/05/03 9:54 horny

OVvGyp http://www.FyLitCl7Pf7kjQdDUOLQOuaxTXbj5iNG.com

# If you are a beginner, it's best to choose a full sole since they provide extra support on the foot which will make it easier to educate yourself on the basic jazz dance steps. " She needed the opposite mothers, her prospective customers, to find o 2017/02/27 1:43 If you are a beginner, it's best to choose a full

If you are a beginner, it's best to choose a full sole since they provide extra support
on the foot which will make it easier to educate yourself on the basic jazz dance steps.
" She needed the opposite mothers, her prospective customers, to find out Carly's outfits. They are extremely fashionable and classy and you cannot make a mistake wearing a pair of these.

# One of the reasons because of this is because they were designed by women designed for women. These are basically purses that have far more pockets and compartments so that you can store you items. Frequently costly bags have a greater quality, being 2017/03/11 14:27 One of the reasons because of this is because they

One of the reasons because of this is because they were
designed by women designed for women. These are basically purses that have far more pockets and compartments so that you can store you items.
Frequently costly bags have a greater quality, being created
from sturdy materials and having an excellent padding.

タイトル
名前
Url
コメント