Skip to content

Instantly share code, notes, and snippets.

View niceume's full-sized avatar

niceume

  • Tokyo
View GitHub Profile
@niceume
niceume / method_implementation_laziness.rb
Created November 14, 2020 13:09
Code that shows it is enough if ruby method implementation can be seen when the method is called.
# In Ruby, unlike many programming languages, calling methods do not need to see the implementation of the method directly. It's enough if those implmentations can be seen when called (executed)
# Note1: Ruby object can extend its functionality with extend.
# Note2: a method given only to a single object is called a singleton method in Ruby.
module Hello
def hello
puts "Hello"
end
end
@niceume
niceume / engine.R
Created March 6, 2020 16:13
Enable sailr in knitr/Rmarkdown
## Sailr script for knitr engine
## I mimicked the code for stan engine
eng_sailr = function(options) {
code = one_string(options$code)
opts = options$engine.opts
##
if (is.null(x <- options$output.var)) {
warning("the option engine.opts$x is deprecated; use the chunk option output.var instead")
x = opts$x
@niceume
niceume / staticinstance.java
Last active August 29, 2015 14:14
Static context and instance context in Java
// Java has static and instance fields.
// Java has static and instance methods.
// Moreover, in code level, Java has static and instance contexts.
// Static methods have static context.
// Static field can be accessed in static context.
//
// Instance methods have instance context.
// Instance field can be accessed in instance context.
// Static field can also be accessed in instance context. // THIS IS AN IMPORTANT PART
@niceume
niceume / classinstance.rb
Created February 2, 2015 04:34
Ruby class instance explanation.
# Class (Class object, eigenclass ) also has its own instance variable(class instance variable).
# It can be accessed in static method.
# By the way, the difference in practice btw class variable and class instance is as follows,
# Class variable is shared by some related objects, say, its instances and inherited classes.
# Class instance variable is an instance variable of only class object.
# In code level, the difference is
# class variable can be written anywhere
# class instance variable can be written in class definition outside of method definitions or in static method definition.
@niceume
niceume / HowToConverGoogleSpreadsheetToCSV.md
Last active August 29, 2015 14:13
How To Conver Google Spreadsheet To CSV

How To Conver Google Spreadsheet To CSV

-1. Make Google spreadsheet accessble from anyone.

  • Share button on the right above.
  • Set "Anyone with the link can view"

-2. (Re)Publish automatically

  • "File > publish to the Web"
@niceume
niceume / Jruby_interface_abstract_class.md
Created January 20, 2015 02:25
Jruby : How to deal with java interface and abstract class

How to deal with java interface and abstract class

@niceume
niceume / JRuby_and_Java_class_conversion.md
Created January 20, 2015 02:23
JRuby and Java class conversion.

JRuby and Java class conversion.

Intro

In JRuby, we can write Ruby code as if we are in real Ruby world. Moreover, From JRuby, most of the methods in Java class can be easily and seamlessly accessed!

One thing I have to keep in mind is that when we pass Ruby objects to Java method, how these objects are converted. And vice versa. (When Java objects are passed to Java methods, I don't have to take care about it. And also Ruby objects passed to Ruby methods.)

Most of the embedded Ruby libraries can be automatically converted to Java coresponding classes.

@niceume
niceume / java_from_jruby.rb
Last active August 29, 2015 14:13
JRuby Tips : Accessing Java class from JRuby
# require "java" provides top-level functions like com, org, java, and javax.
require "java"
###############################
# Access Java class. (with package name (Ruby namespace))
###############################
java.lang.StringBuffer
# In JRuby, this class is recognized as
# Java::JavaLang::StringBuffer
# The begining Java:: means
# This is a code for JRuby.
#
# Put the commons-math3-3.4.1.jar in the current code.
# Run
# $ jruby -J-cp commons-math3-3.4.1.jar TTESTStat.rb
#
require "java"
module ACMStat
@niceume
niceume / TTestStat.java
Created January 15, 2015 22:30
Run ttest in Java by using Apache Math Library
// Put the commons-math3-3.4.1.jar file in the same directory of this code.
// Run :
// $ javac -cp commons-math3-3.4.1.jar TTESTStat.java
// $ java -cp .:commons-math3-3.4.1.jar TTESTStat
//
// When running "java", you have to be careful about "-cp option" having ".:" in its argument.
//
// "-cp" option clears previous CLASSPATH setting.
// When running javac, TTESTStat.java means a file on filesystem (Not a java class yet. Still just a file at this point).
// So ".:" is not required.