8/2学んだこと / Ruby@codecademy / 英語


codecademy のRuby講座を90%まで進めた。今日カバーしたところは経験した言語にない、または似てるけど異なる要素があって面白かった。

  • Zen of Ruby

    There's always more than one way to do something in Ruby.

    Java, Pythonとは違う精神。Perlでは馴染みづらかったけど、Rubyはいまのところそうでもない。

  • Block

    These are similar to anonymous functions in JavaScript or lambdas in Python.

    A method can take a block as a parameter.

  • Hash

    What happens if you try to access a key that doesn't exist, though? In many languages, you'll get an error of some kind. Not so in Ruby: you'll instead get the special value nil.

  • Symbol

    Symbols make good hash keys for a few reasons:

    They're immutable, meaning they can't be changed once they're created;

    Only one copy of any symbol exists at a given time, so they save memory;

    Symbol-as-keys are faster than strings-as-keys because of the above two reasons.


    hash lookup is faster with symbol keys than with string keys.

  • Conditional Assignment


  • Implicit Return


    Explicit is better than implicit. PEP 20 -- The Zen of Python |

  • Yield


  • Proc

    You can think of a proc as a "saved" block: just like you can give a bit of code a name and turn it into a method, you can name a block and turn it into a proc.

  • The Ruby Lambda

    Like procs, lambdas are objects. The similarities don't stop there: with the exception of a bit of syntax and a few behavioral quirks, lambdas are identical to procs.


    If you're thinking that procs and lambdas look super similar, that's because they are! There are only two main differences.

    First, a lambda checks the number of arguments passed to it, while a proc does not. This means that a lambda will throw an error if you pass it the wrong number of arguments, whereas a proc will ignore unexpected arguments and assign nil to any that are missing.

    Second, when a lambda returns, it passes control back to the calling method; when a proc returns, it does so immediately, without going back to the calling method.

  • Class

    When dealing with classes, you can have variables that are available everywhere (global variables), ones that are only available certain methods (local variables), others that are members of a certain class (class variables), and variables that are only available to particular instances of a class (instance variables).

# instance variables
# class variables
# global variables
$name # or define it outside of any method or class

残りはObject-Oriented Programmingのパートを少し。