Skip to content

Instantly share code, notes, and snippets.

require "minruby"
def evaluate(tree, env)
# pp(tree)
case tree[0]
when "lit"
tree[1]
when "+"
evaluate(tree[1], env) + evaluate(tree[2], env)
when "-"
# $ ruby interp.rb test.rb
# test.rb
# plus_count=0
# x = 1+2+3
# p(plus_count) #=> 2
# x = 1+2+3
# p(plus_count) #=> 4
require "minruby"
class A
def foo
"A"
end
end
class B < A
def foo
"B" + super
end
end
# ruby 2.7.0preview2 (2019-10-22 master 02aadf1032) [x86_64-darwin18]
class Foo
private def foo
"foo!!"
end
def bar
self.foo
end
end
# Rubyでメソッドと、そうではないもの(式とか)の見分け方
# methodメソッドで取得できたらメソッド
method(:p) #=> #<Method: main.p>
# ↑とれたのでメソッド
method(:for)
#=> NameError: undefined method `for' for class `#<Class:#<Object:0x00007fd82d0d22f0>>'
# ↑とれないのでメソッドではない
# $ gem i selenium-webdriver
require "selenium-webdriver"
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument('--headless')
# headless指定すると普段Chromeで指定しているフォント指定が効かなくなる
driver = Selenium::WebDriver.for :chrome, options: options
driver.get("https://igarashikuniaki.net/diary/")
driver.save_screenshot("ss.png")
## rubyforge
$ gem sources -l
*** CURRENT SOURCES ***
http://gems.rubyforge.org/
$ gem i awesome_print
ERROR: While executing gem ... (TypeError)
# enc_test.txt に 日本語 を1行書いておく
File.open("enc_test.txt") do |f|
puts out_str = f.each_line.first
puts out_str.encoding
end
puts in_str = "中の日本語"
puts in_str.encoding
module Hoimirable
def hoimi
@hp = [@hp + 20, @max_hp].min
end
end
class Sumaltria
include Hoimirable
end
h1 = {}
h1.default= []
h1[:a] = h1[:a] << "hi"
h1[:b] = h1[:b] << "ho"
p h1 #=> {:a=>["hi", "ho"], :b=>["hi", "ho"]}
h2 = {}
h2.default_proc = ->(hash, key){ [] }
h2[:a] = h2[:a] << "hi"
h2[:b] = h2[:b] << "ho"