Everything is an object-全てはオブジェクト-RubyMonkより[Ruby][和訳]

Hey guys:)
早速だけど今日も和訳をするぜ!今日の記事は残念ながらstack over flowからのものではない。
Railsの公式チュートリアルRubyの勉強できるサイトのリンクがいくつか貼ってあったので、
そこからみつけたRubyMonkっていうところから抜粋してみた。
まだオブジェクト指向に馴染めていないのでまず基本的なオブジェクトとは?のところに注目してみた。
ただし、今回訳すのは導入部分のみです。この導入部分で続きが気になったら下のリンクから続きを参照願います。

–タイトル:Everything is an object(全てはオブジェクトである)–

In Ruby, just like in real life, our world is filled with objects.

Rubyは現実世界と同様にオブジェクトで溢れている。

Everything is an object - integers, characters, text, arrays - everything.

全てはオブジェクトである。整数、文字、 文、 配列のように全てだ。

To make things happen using Ruby, one always puts oneself in the place of an object and then has conversations with other objects, telling them to do stuff.

Rubyを使うのなら機能を持たせたオブジェクトを作り、その作られたオブジェクトが他のオブジェクトに命令を与えさせるようにするんだ。

Roleplaying as an object in your program is an integral part of object-oriented programming.

自分のプログラミングでオブジェクトを機能させるのはオブジェクト指向プログラミングには欠かせないものである。

To know which object you are at the moment, one may use the keyword self.

どのオブジェクトを現在使っているか知るには、selfというキーワードを使おう。

Try it for yourself.

自分でやってみよう。

(補足)
  
[1] pry(main)> self
=> main

As you can see, if you don’t specify which object you are, you automatically play the role of the main object that Ruby provides us by default.

君がみたように、どのオブジェクトを使っているか明記しなければ自動的にmainオブジェクトを使っていることになっている。これはRubyがデフォで搭載したものだ。

どうでしょう??
これはまだオブジェクト指向について完全に説明されてないが、
記事によって説明の仕方が大分異なったりするので取っ付きやすいと思ったら下のリンクからどうぞ。

See you:)

RubyMonk - Ruby Primer - Introduction to Objects

配列の中に値が入っているか確認[和訳]

hey, guys :)
さて、今日も和訳すっか!
今回は質問者に859、回答者に1331のいいね!がついている人気の記事である。
そんなにいい質疑応答だったのか!?って気になるよね。

ーー質問ーー

I have a value ‘Dog’ and an array [‘Cat’, ‘Dog’, ‘Bird’].

訳)'Dog'という値と[‘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

訳)君が探しているのはinclude?だ。

>> ['Cat', 'Dog', 'Bird'].include? 'Dog'
=> true

–終わり–

え?これだけ?w
いいね!が多い記事は難しい話に突っ込むようなイメージがあったが、シンプルなのが意外に明白で評判いいのかもしれない。

この記事を読んだ時最初は拍子抜けだったが、あっさりすぎて次第にエンジニア初心者の私に勇気を与えてくれているような気がした。

ありがとう、user211662(質問者)とBrian Campbell(回答者)。君たちのあっさりさは人に勇気を与えた。

Thank you user211662 and Brian Campbell. Your simplenesses are giving me a hope and courage.
Hope you two will check my blog someday. God bless you.

end

原文

Check if a value exists in an array in Ruby - Stack Overflow

Rubyのattr_accessorって何?[和訳]

hey guy:) 前回から引き続きstackoverflowの記事を共有したいなと思ったので和訳してみた。
こちらの記事は質問者に697、回答者に1691の"いいね"されている。基本的な質問かもしれないけど読みたくなるよね!

–質問–
I am having a hard time understanding attr_accessor in Ruby.
Can someone explain this to me? I have done tons of Google searches.
I just can’t understand it fully.

訳)Rubyのattr_accessorってなんやねん。誰か説明してくれへんか?ググってもみつからへんわ〜。理解できへん。

–回答–

Let’s say you have a class Person.

Personクラスがあったとしよう。

class Person
end

person = Person.new
person.name # => no method error

Obviously we never defined method name.

明らかにnameメソッドを定義していない。

Let’s do that.

定義してみよう。

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  

Aha, we can read the name, but that doesn’t mean we can assign the name.

よし、nameメソッドを読み込めた。だが名前を割り当てたわけではない。

Those are two different methods.

2つの別なメソッドがある。

Former called reader and latter called writer.

前者はreaderを後者はwriterを読み込んでいる。

We didn’t create the writer yet so let’s do that.

まだ記述されていないwriterを書いてみよう。

class Person
  def name
    @name
  end

  def name=(str)
    @name = str
  end
end

person = Person.new
person.name = 'Dennis'
person.name # => "Dennis"

Awesome. Now we can write and read instance variable @name using reader and writer methods.

よっしゃ。ついにreaderとwriterメソッドを使うことによってインスタンス変数(@name)を定義して呼び出すことができた。

Except, this is done so frequently, why waste time writing these methods every time?

これはよく使われる。だが、毎回こんなに記述するって時間もったいないと思わないかい?

We can do it easier.

もっと簡単にすることが出来るんだ。

class Person
  attr_reader :name
  attr_writer :name
end

Even this can get repetitive. When you want both reader and writer just use accessor!

まだ面倒くさい感じに書かれている。もしreaderとwriterを同時に書くならaccessorを使おう!

class Person
  attr_accessor :name
end

person = Person.new
person.name = "Dennis"
person.name # => "Dennis"

Works the same way!

同じ結果が返ってきた!

And guess what: the instance variable @name in our person object will be set just like when we did it manually, so you can use it in other methods.

考えてみて。インスタンス変数(@name)はpersonオブジェクトに設定することが出来る。先程私達が頑張ってやったように。
なのでそのオブジェクトを他のメソッドからも呼び出せるよ。

class Person
  attr_accessor :name

  def greeting
    "Hello #{@name}"
  end
end

person = Person.new
person.name = "Dennis"
person.greeting # => "Hello Dennis"

That’s it. こんな感じ。  

追記)readerとwriterはそれぞれゲッター、セッターと呼ばれている。

なるほど〜。実際にターミナルで試した方が感動する!
元となるコードから辿っていくのっておもしろいね!

See you!!:)

リンク

What is attr_accessor in Ruby? - Stack Overflow

Rubyのシンボルとは[和訳]

hi, long time not to see you guys:)

Rubyのハッシュってコードの書き方はわかるんだけど、いまいちピンとこない。
stackoverflowで調べてみてなんとなくわかったような気がした回答があったので和訳してみた。

–質問–

patient1 = { :ruby => "red" }
patient2 = { :ruby => "programming" }

patient1.each_key {|key| puts key.object_id.to_s}
3918094
patient2.each_key {|key| puts key.object_id.to_s}
3918094

上記のコードってどういう意味があるんだ?

–回答–
The symbol :ruby does not contain “red” or “programming”.

シンボル:rubyに"red"と"programming"という文字列はもたない。

The symbol :ruby is just the symbol :ruby.

シンボル:rubyはただのシンボル:rubyである。

It is your hashes, patient1 and patient2 that each contain those values, in each case pointed to by the same key.

それは君が作ったハッシュでpatient1とpatient2はそれぞれにバリューを持っている。それぞれの事象であればハッシュは同じキーを示している。

Think about it this way: If you go into the living room on christmas morning, and see two boxes with a tag on them that say “Kezzer” on them.
こう考えてみよう。もしあなたがクリスマスの朝にリビングルームへ行ったとしよう。そこには2つの箱があって、それぞれに"Kezzer"と書かれているタグがついている。

One has socks in it, and the other has coal.

片方には靴下が入っていて、もう片方には木炭が入っている。

You’re not going to get confused and ask how “Kezzer” can contain both socks and coal, even though it is the same name.

靴下と木炭がそれぞれの箱に入っているが、同じ名前である"Kezzer"タグが付いていても混同しないだろう。

Because the name isn’t containing the (crappy) presents.

なぜなら名前それ自体には(くだらない)プレゼントが含まれていないからだ。

It’s just pointing at them.   

名前はただそれらを示しているだけにすぎない。

Similarly, :ruby doesn’t contain the values in your hash, it just points at them.

同様に、:rubyは貴方が記述した値はハッシュに含まれない。ただ示しているだけだ。

はは
なんかオシャレな例えだねw See you :)

記事)

Understanding Symbols In Ruby - Stack Overflow

英語のおしゃれ表現を発見

hey, guys:)!!

やばい。。最近のエンジニア業で何もできていない汗汗
I thought the current task is simple in the first place but I feel asshole what I did these days.. So I blog about the fancy English which is beyond my level.
*日本語訳)今取り組んでいる課題は簡単だと思ってたんだぜ、最初はな。でもそうは上手く行かないんだ。自分のことをassholeだと思ってしまう。だからな、今日は自分では思い浮かばない英語表現を発見したんだ。これからそのことについてブログっちゃうんだ。

単語帳でボキャブラリを増やしたり、文法書を読んで英語の構文を理解するのは英語を勉強する上で重要だと思う。 ただガチガチの英語表現しか書かれていないと実際のネイティブはどのように話しているのかイメージが遠のいてしまう。なので気分晴らしに洋画をみて実際の生きた英会話を聞いてみてはどうだろうか。はっは〜、と関心してしまう表現に出会えるはずだ。
最近私が関心した英語表現を今回は一つ紹介したい。
これは映画"Gone Girl"から発見しました。
新しい隣人に対してアメリカ人らしくフランクに話しかけるシーンです。

Brenda "Hey, neighbor. It's been weeks since I had anyone decent next door."  (隣人さん、まともな人が引っ越してきたのは久しぶりだわ)

まずこれオシャレなところが入っていると思いませんか? 私は"It's been weeks"っていうところがオシャレだなと思った。
何週間ではなく何ヶ月もしくは1年近くいなかったはず、もしくはまともな人は一度もいなかったのに数週間という風に言っているのがcool!!
その返しで新しい隣人のAmyはなんて返しただろう。
先に話しておくと日本語訳は「自分はまともなんかじゃないよ」って返していた。
自分だったら「Thank you, but I'm not like that.」とか、「Do you think so? But unfortunately I'm not...」的な感じでboringな英語表現で返していただろう。
ところがやはりAmyは違った。。

Amy "I don't know how decent I feel."  

これ、かなりcoolじゃありませんか?
ネイティブじゃないと絶対に思い浮かばない表現方法に感動した。
どこがcoolかは言葉の並べ方にあるのだと思う。
自分だったら「I don't know a feeling of decent.」とか「I've never been decent person.」といった表現で、なんかガチガチの表現にしかなりえない。

共感できますか??ww

自分の大好きな映画を何百回観たり、聞いたりするのもかなり大事な英語の勉強法だと気づいた。気に入った表現をみつけたら自分なりにアレンジして使ってみるとかなりcoolになれるよ!

See you!!:)

ストロングパラメーターで情報を制限! Ruby on Rails

Hey guys:)

ストロングパラメータで取ってくる情報を制限しよう!
まずはストロングパラメータを使ってないところから始めるお

def create  
  @article = Article.new(params[:article])  
                       .  
                       .
end

よし、それではストロングパラメータでタイトル(title)と本文(content)を取ってみよう!!

def create  
  @article = Article.new(params.require(:article).permit(:title, :content))  
                                .  
                                .  
end  

おっ!なんかストロングになった気がしません? requireとpermitって単語使っているあたりが強そう!
require(:テーブル).permit(:カラム, :カラム)っていう感じだね!

さらにメソッドにしちゃえ!!

def create  
  @article = Article.new(article_params)  
                       .   
                       .  
end  

def article_params  
   @article = Article.new(params.require(:article).permit(:title, :content))  
                       .  
                       .  
end  

みたいな感じでね!!

同ファイル内でしか呼び出さないのならprivateをarticle_paramsの上に記述して制限するのもいいかも。

See you !!:)

-Ruby on rails-migrationでデータベースの構造を変えてみよう

hi, guys:)

migrationというコマンドでデータベースの内容を変えることができる。
migrationって聞いた時は移民、移住といった言葉しか浮かばずしっくりこなかった。
それではさっそくmigrationしてみよう。
入力するのはもちろんターミナルで:)

$ bin/rails g migration add_user_id_to_myapp user_id:integer

後半の"add_user_id_to_myapp"はファイル名。
"user_id:integer"はカラム名:データ型といった感じです(integer = 整数)。
出来上がったファイル名は「20160531175330_add_user_id_to_myapp」。
前半の数字は作成時刻(タイムスタンプ)で後半はコマンドで入力したファイル名である。
ファイルの中を見てみよう。

class AddUserIdToMyapp < ActiveRecord::Migration  
  
  def change  

    add_column :myapp, :user_id, :integer  

  end  
  
end

こんな感じです。
それでは最後にデータベースに反映させよう!

$ rake db:migrate

これで自分の作成したファイルがデータベースへ移民(反映)しました。
これでmigrationの言葉の意味も結びついたはず!

See you :)