This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def block_sample | |
| puts 'start up' | |
| yield if block_given? | |
| puts 'sit down' | |
| end | |
| block_sample |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| languages = %w(Perl Python Ruby Smalltalk JavaScript) | |
| languages.each do |language| | |
| puts language | |
| if language == 'Ruby' | |
| puts 'I found Ruby!!' | |
| break | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| for name in %w(Alice Bob Carol) | |
| puts name | |
| end | |
| for val in {a: 1, b: 2} | |
| puts val[0] | |
| puts val[1] | |
| end | |
| for key, val in {a: 1, b: 2} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| languages = %w(Perl Python Ruby) | |
| i=0 | |
| while i < languages.length | |
| puts "Hello, #{languages[i]}." | |
| i += 1 | |
| end | |
| i = languages.length - 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| stone ="ruby" | |
| case stone | |
| when 'ruby' | |
| puts '7月' | |
| when 'peridot','sardonyx' | |
| puts '8月' | |
| else | |
| puts 'よくわかりません' | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| n = 0 | |
| puts '0でした' if n.zero? | |
| puts '0ではありませんでした' unless n.zero? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyObject | |
| def ==(other) | |
| self.class == other.class | |
| end | |
| end | |
| result = MyObject.new == MyObject.new | |
| puts result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mycode | |
| object App { | |
| def main(args : Array[String]) { | |
| trait SentientBeing | |
| trait Animal extends SentientBeing | |
| case class Dog(name: String) extends Animal | |
| case class Person(name: String, age: Int) extends SentientBeing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mycode | |
| case class Person(name :String) | |
| object App { | |
| def main(args : Array[String]) { | |
| def speak(p: Person) = p match { | |
| case Person(name) if name == "Fred" => println("Yubba dubba doo") | |
| case _ => println("Watch the Flintstones!") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mycode | |
| trait Animal | |
| case class Dog(name: String) extends Animal | |
| case class Cat(name: String) extends Animal | |
| case object Woodpecker extends Animal | |
| object App { | |
| def main(args : Array[String]) { | |
| def determineType(x: Animal): String = x match { |