Archive for: 󈥬月 2016’

Ruby と Python と Elixir と。

2016年12月30日 Posted by PURGE

除算の違い。

# Ruby
puts 3.to_f / 2 #=> 1.5
puts 3 / 2      #=> 1

# Python
print(3 / 2) # => 1.5
print(3 // 2) # => 1

# Elixir ※1.4以降
require Integer
IO.puts(3 / 2) # => 1.5
IO.puts(Integer.floor_div(3, 2)) # => 1

剰余の違い。

# Ruby
puts 5 % 2 #=> 1

# Python
print(5 % 2) # => 1

# Elixir ※1.4以降
require Integer
IO.puts(Integer.mod(5, 1)) # => 1

べき乗の違い

# Ruby
puts 2 ** 3 #=> 8

# Python
print(2 ** 3) # => 8

# Elixir ※Erlangの関数使用
IO.puts(:math.pow(2,3)|> round) # => 8

React 事始め

2016年12月12日 Posted by PURGE

JavaScriptのフレームワークとして、Angularを利用していたが、Reactも気になったのでサンプル。
ライブラリは、CDNを使用するが、下記の記述で動作する。

babel等のバージョン違い等によって動作しなくなるので悩ましい。

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <script src="https://unpkg.com/react@15/dist/react.min.js"></script>
    <script src="https://unpkg.com/react-dom@15/dist/react-dom.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.38/browser.min.js"></script>

    <title>React Sample</title>
</head>
<body>
<div id="root">
    <!-- This div's content will be managed by React. -->
</div>

<script type="text/babel">
    ReactDOM.render(
            <h1>こんにちは。たぢさん。</h1>,
        document.getElementById('root')
    );
</script>

</body>
</html>

JSファイルを外出しにする場合は、下記の様に変更する。

<script type="text/babel" src="js/common.js"></script>

また、自作のコンポーネントを作成して表示する。

//コンポーネントのレンダリング
var MyDiv = React.createClass({
    displayName : '表示されるかな?',
    render: function(){
        return (
            <p>わたしはどこへ?</p>
        );
    }
});
ReactDOM.render(
    <MyDiv></MyDiv>,
    document.getElementById('mydiv')
);

HTMLの記述は下記。

<div id="mydiv"></div>

次に、コンポーネントへパラメータを渡してみる。


var MyDiv2 = React.createClass({
    displayName: 'パラメータ渡し',
    render: function () {
        return(
            <p>パラメータは<span style={{color:'red'}}>{this.props.param}</span>です。</p>
        );
    }
});

ReactDOM.render(
    <MyDiv2 param="引数"></MyDiv2>,
    document.getElementById('mydiv2')
);

HTMLの記述は下記。

<div id="mydiv2"></div>

スクリーンショット 2016-12-12 13.13.11

続いて、イベントを発生させてみる。

var MyButton = React.createClass({
    displayName: 'アラート',
    clickHandler: function () {
        alert('クリックされました。');
    },
    render: function () {
        return(
            <input type="button" value="クリック" onClick={this.clickHandler}/>
        );
    }
});
ReactDOM.render(
    <MyButton></MyButton>,
    document.getElementById('mybtn')
);

HTMLの記述は下記。

<div id="mybtn"></div>

さらに、SELCTボックスの値を変更した場合の挙動を試してみる。
ミソは、getInitialState と this.setState である。

var MySelect = React.createClass({
    selValue: '',
    displayName: 'セレクタ',
    //State初期化
    getInitialState: function () {
        return {val: ''};
    },
    selectChangeHandler: function (event) {
        this.selValue = event.target.value;
        //State更新
        this.setState({
            val: this.selValue
        });
    },
    render: function () {
        return(
            <div>
                <div>
                    <select onChange={this.selectChangeHandler}>
                        <option value='1'>Java</option>
                        <option value='2'>Ruby</option>
                        <option value='3'>Python</option>
                        <option value='4'>JavaScript</option>
                        <option value='5'>C#</option>
                    </select>
                </div>
                <p>VALUE: {this.state.val}</p>
            </div>
        );
    }
});
ReactDOM.render(
    <MySelect></MySelect>,
    document.getElementById('myselect')
);

HTMLの記述は下記。

<div id="myselect"></div>

Ruby のclass

2016年12月10日 Posted by PURGE

いくつも言語をやっていると、記述方法を忘れるので覚え書き。

■ animal.rb

class Animal
  attr_accessor :name
  def initialize(name)
    @name =  name
  end
  def bark()
    puts "私は#{@name}です。"
  end
end

■ sample.rb

require './animal.rb'

dog = Animal.new("犬")
cat = Animal.new("猫")

animals = [dog, cat]
animals.each { |animal|
  animal.bark
}
# => 私は犬です。
# => 私は猫です。

bundle install が nokogiri のライブラリ依存で失敗した場合

2016年12月6日 Posted by PURGE

bundle install が、nokogiri のライブラリ依存で失敗した場合。

Installing nokogiri 1.6.8.1 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /Users/xxxxx/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/nokogiri-1.6.8.1/ext/nokogiri
/Users/xxxxx/.rbenv/versions/2.3.1/bin/ruby -r ./siteconf20161206-45292-57wgxr.rb extconf.rb
checking if the C compiler accepts ... yes
checking if the C compiler accepts -Wno-error=unused-command-line-argument-hard-error-in-future... no
Building nokogiri using packaged libraries.
Using mini_portile version 2.1.0
checking for iconv.h... yes
checking for gzdopen() in -lz... yes
checking for iconv... yes
************************************************************************
IMPORTANT NOTICE:

Building Nokogiri with a packaged version of libxml2-2.9.4.

Team Nokogiri will keep on doing their best to provide security
updates in a timely manner, but if this is a concern for you and want
to use the system library instead; abort this installation process and
reinstall nokogiri as follows:

    gem install nokogiri -- --use-system-libraries
        [--with-xml2-config=/path/to/xml2-config]
        [--with-xslt-config=/path/to/xslt-config]

If you are using Bundler, tell it to use the option:

    bundle config build.nokogiri --use-system-libraries
    bundle install

Note, however, that nokogiri is not fully compatible with arbitrary
versions of libxml2 provided by OS/package vendors.
************************************************************************

brewで、libxml2 をインストールして、bundleのオプションで、libxml2 のライブラリの場所を指定する。

$ brew install libxml2
$ bundle config build.nokogiri --use-system-libraries --with-xml2-include=/usr/local/opt/libxml2/include/libxml2/
$ bundle install

JavaScriptで、substrとsubstring

2016年12月6日 Posted by PURGE

超久しぶりで、JavaScript の文法おさらい。

var str = "こんにちは。たぢさん。";
console.log(str.length);
console.log(str.substr(6,4));       //文字数指定
// => たぢさん
console.log(str.substring(6,10));    //文字位置指定
// => たぢさん