Deleted articles cannot be recovered. Draft of this article would be also deleted. Are you sure you want to delete this article?
Rubyã§ã¯ï¼ŒStructクラスを用ã„ã‚‹ã“ã¨ã«ã‚ˆã£ã¦ï¼Œç‰¹å®šã®ãƒ•ã‚£ãƒ¼ãƒ«ãƒ‰ã‚’æŒã¤ã‚¯ãƒ©ã‚¹ã‚’ç°¡å˜ã«ä½œæˆã™ã‚‹ã“ã¨ãŒã§ãã¾ã™ã€‚ Foo = Struct.new("Foo", "foo", "bar") f = Foo.new(1, 2) p f.foo # 1 p f.bar # 2 一方ã€Pythonã®å ´åˆã€namedtupleã¨ã„ã†ãƒ©ã‚¤ãƒ–ラリã§åŒæ§˜ã®æ©Ÿèƒ½ãŒæä¾›ã•ã‚Œã¦ã„ã¾ã™ã€‚ namedtupleã¯Python2.6以é™ã§åˆ©ç”¨å¯èƒ½ã§ã™ã€‚ from collections import namedtuple Foo = namedtuple("Foo", "foo bar") f = Foo(1, 2) print f.foo # 1 print f.bar # 2 ãŸã ã—ã€Pythonã§ã¯tupleãŒã‚¤ãƒŸãƒ¥ãƒ¼ã‚¿ãƒ–ル(書ãæ›ãˆä¸å¯èƒ½)ãªã‚ªãƒ–ジェクトã§ã‚ã‚‹ã®ã¨åŒæ§˜ã«ã€ã“ã®namedtupleも属性ã®å€¤
Scalaã®case classãŒå¥½ã。一行ã±ã±ã£ã¨æ›¸ãã ã‘ã§åž‹ã®æ©æµã‚’å—ã‘られるã®ãŒæœ€é«˜ã€‚ Rubyã§ã‚‚ãã†ã„ã†ã®ãŒæ¬²ã—ããªã£ãŸã®ã§ç¬¬ä¸€å¼¾ã¨ã—ã¦å˜ç´”ãªã‚¤ãƒŸãƒ¥ãƒ¼ã‚¿ãƒ–ルãªã‚³ãƒ³ãƒ†ãƒŠã¨ã—ã¦ã®åž‹ã‚’定義ã—ã¦ã¿ã‚‹ã€‚ å˜ç´”ã«ãƒãƒƒã‚·ãƒ¥ã‚’使ã†ã¨ã‹hashieを使ã†ã¨ã‹ã§ã‚‚çµæžœã®ç”¨é€”ã¨ã—ã¦åŒã˜ã‚ˆã†ãªã“ã¨ãŒã§ãã‚‹ã‘ã©åž‹ãŒæ¬²ã—ã„ã®ã§ã™ã€‚ 追記 コメントã§æ•™ãˆã¦ã‚‚らã„ã¾ã—ãŸãŒã€Struct ã§ã‚„ã‚ŠãŸã„ã“ã¨ãŒã§ãã‚‹ã®ã§ã—ãŸã€‚Qiitaã«æ›¸ã„ã¦ã‚ˆã‹ã£ãŸï¼ 追記ã“ã“ã¾ã§ 普通ã«æ›¸ãã¨ã“ã‚“ãªæ„Ÿã˜ã«ãªã‚‹ã¯ãšã€‚ class Food attr_reader :price, :name def initialize(price, name) @price = price @name = name end end def case_class(*args) claz = Class.new claz.send :define_me
OpenStruct è¦ç´ ã‚’å‹•çš„ã«è¿½åŠ ・削除ã§ãる手軽ãªæ§‹é€ 体をæä¾›ã™ã‚‹ã‚¯ãƒ©ã‚¹ã§ã™ã€‚ OpenStruct ã®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã«å¯¾ã—ã¦æœªå®šç¾©ãªãƒ¡ã‚½ãƒƒãƒ‰ x= を呼ã¶ã¨ã€ OpenStruct クラス㮠BasicObject#method_missing ã§æ•æ‰ã•ã‚Œã€ãã®ã‚¤ãƒ³ã‚¹ã‚¿ãƒ³ã‚¹ã« インスタンスメソッド x, x= ãŒå®šç¾©ã•ã‚Œã¾ã™ã€‚ ã“ã®æŒ™å‹•ã«ã‚ˆã£ã¦è¦ç´ ã‚’å‹•çš„ã«å¤‰æ›´ã§ãã‚‹æ§‹é€ ä½“ã¨ã—ã¦åƒãã¾ã™ã€‚ 手軽ã«ä½¿ãˆã‚‹ require "ostruct" config = OpenStruct.new config.level #=> nil config.level = :debug config.level #=> :debug
å¤ã良ãå°å¦æ ¡ã®æ™‚代ã€ã“ã®è¡Œã«ã¯å›°æƒ‘ã•ã›ã‚‰ã‚ŒãŸã‚‚ã®ã ã£ãŸã€‚ é”術的㪠x ãŒã€åŠ ç®—ã•ã‚ŒãŸã®ã«ç‰ã—ã„ã¾ã¾ã§ã„る事ã«ã€‚ ã©ã†ã„ã†ã‚ã‘ã‹ã€ãƒ—ãƒã‚°ãƒ©ãƒŸãƒ³ã‚°ã‚’始ã‚ã‚‹ã¨ã€ãã‚Œã«æ§‹ã‚ãªããªã‚‹ã€‚ 「やれやれã€ãã‚Œã¯é‡å¤§ãªäº‹æŸ„ã˜ã‚ƒãªã„ã—ã€ãƒ—ãƒã‚°ãƒ©ãƒŸãƒ³ã‚°ã¨ã¯ç¾å®Ÿã®ãƒ“ジãƒã‚¹è¡Œç‚ºãªã‚“ã ã‹ã‚‰ã€ æ•°å¦çš„ãªç´”粋ã•ã«ã¤ã„ã¦ã‚ら探ã—ãªã‚“ã¦å¿…è¦ç„¡ã„ (ãã®è°è«–ãªã‚‰ã€å¤§å¦ã«ã„ã‚‹ç‹‚ã£ãŸé«é¢é‡ŽéƒŽã©ã‚‚ã«ã•ã›ã¦ãŠã‘ã°ã„ã„)ã€ã¨æ€ã£ã¦ã„ãŸã€‚ ã‘ã‚Œã©ã‚‚ã€ãŸã 知らãªã‹ã£ãŸã ã‘ã§ã€æˆ‘々ãŒé–“é•ã£ã¦ã„ã¦é«˜ã„代償を支払ã£ã¦ã„ãŸã®ã¯ 明らã‹ã§ã‚る。 Wikipedia ã«ã‚ˆã‚Œã°ã€ã€Œé–¢æ•°åž‹ãƒ—ãƒã‚°ãƒ©ãƒŸãƒ³ã‚°(functional programming, FP)ã¨ã¯ã€ 計算を数å¦çš„ãªé–¢æ•°ã®è©•ä¾¡ã¨ã¿ãªã—〠状態やå¯å¤‰ãƒ‡ãƒ¼ã‚¿ã‚’é¿ã‘るプãƒã‚°ãƒ©ãƒŸãƒ³ã‚°ãƒ‘ラダイムã€ã§ã‚る。 言ã„æ›ãˆã‚‹ã¨ã€é–¢æ•°åž‹ãƒ—ãƒã‚°ãƒ©ãƒŸãƒ³ã‚°ã¯ã€ 副作用ãŒç„¡ã変数ã®å€¤ã‚’変化ã•ã›ãªã„コードを推奨ã™ã‚‹ã€‚
This document is compiled from RubyFunctionalProgramming by Arnau Sanchez (tokland) Japanese version is also available. Table of Contents Introduction The theory Functional programming in Ruby Don't update variables Blocks as higher order functions OOP and funcional programming Everything is an expression Recursion Lazy enumerators A practical example Conclusion Presentations Further reading Intro
自分ã®ãƒ©ã‚¤ãƒ–ラリ㧠String ãªã©ã®åŸºæœ¬çš„ãªã‚¯ãƒ©ã‚¹ã‚’オープンã—ã¦ãƒ¡ã‚½ãƒƒãƒ‰ã‚’è¿½åŠ ã™ã‚‹å ´åˆã®æ›¸ãæ–¹ã«ã¤ã„ã¦ã€activesupport-2.3.2ã€activesupport-3.2.3ã€zucker-12.1ã€facets-2.9.3 ã§ã©ã†ã‚„ã£ã¦ã„ã‚‹ã‹ã‚’調ã¹ã¾ã—ãŸã€‚ activesupport-2.3.2 ã®å ´åˆ â–¼ activesupport-2.3.2/lib/active_support/core_ext/string.rb require 'active_support/core_ext/string/filters' ... class String #:nodoc: ... include ActiveSupport::CoreExtensions::String::Filters ... end â–¼ activesupport-2.3.2/lib/active_suppo
2.1.1 :001 > require 'active_support' => true 2.1.1 :002 > p 'cat'.pluralize NoMethodError: undefined method `pluralize' for "cats":String from (irb):2 from /Users/kazuhiro.serizawa/.rvm/rubies/ruby-2.1.1/bin/irb:11:in `<main>' ã¨ã‚Šã‚ãˆãšå…¨éƒ¨ãƒãƒ¼ãƒ‰ã—ãŸã„(ã‚ã¾ã‚Šã‚ˆã‚ã—ããªã„ãŒï¼‰ã¨ã„ã†æ™‚ã¯ä»¥ä¸‹ã®ã‚ˆã†ã«ã™ã‚‹ 追記 コメントã§ã”指摘をå—ã‘ã¦requireæ–‡ã®å†…容を修æ£ã—ã¾ã—ãŸã€‚ã‚ã‚ŠãŒã¨ã†ã”ã–ã„ã¾ã—ãŸï¼ 追記ã®è¿½è¨˜ å…¬å¼çš„ã«ã¯æœ€åˆã«active_supportã‚‚requireã—ã¨ã„ãŸæ–¹ãŒã„ã„らã—ã„ã®ã§ä¿®æ£ã—ã¾ã—ãŸã€‚
# encoding: utf-8 require 'facets/kernel/try' #------------------------------------------------------------------------------- # try: 後ã‚ã«ç¶šãメソッドをã€é€šå¸¸é€šã‚Šã«å‘¼ã³å‡ºã™ãŒ # nilã«å¯¾ã—ã¦å˜åœ¨ã—ãªã„メソッドを呼ã³å‡ºã—ãŸå ´åˆã§ã‚‚エラーã«ãªã‚‰ãªã„(nilã‚’è¿”ã™ï¼‰ # # 主ã«ã€Œnilã‹ã‚‚ã—ã‚Œãªã„変数ã€ã«å¯¾ã—ã¦ã€ãªã«ã‹ã®ãƒ¡ã‚½ãƒƒãƒ‰ã‚’呼ã³å‡ºã—ãŸã„ã¨ãã«ä½¿ã† options = {:output_path => ' /var/gems/1.9.1/ '} @stripped_path = options[:output_path].try.strip p @stripped_path # => "/var/gems/1.9.1/" options = {} @s
# encoding: utf-8 require 'facets/time' time = Time.new(2013, 2, 5, 17, 42, 50) p time # => 2013-02-05 17:42:50 +0900 #------------------------------------------------------------------------------- # Time.elapse: ブãƒãƒƒã‚¯ã®å®Ÿè¡Œæ™‚間を計測ã™ã‚‹ p Time.elapse{ sleep(1) } # => 1.0000581741333008 #------------------------------------------------------------------------------- # stamp: 対象ã®æ—¥æ™‚を指定ã—ãŸãƒ•ã‚©ãƒ¼ãƒžãƒƒãƒˆã§æ–‡å—列化 # Time.st
今回ã¯ã€ActiveSupportã®yesterdayメソッドを例ã«ã‚„ã£ã¦ã¿ã¾ã™ã€‚ ActiveSupportã®yesterdayメソッドã¯ã€åå‰ç©ºé–“全部å«ã‚ã‚‹ã¨ã€ActiveSupport::CoreExtensions::Date::Calculations.yesterdayã§ã—ãŸã€‚ https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/calculations.rb ãªã®ã§ã€ã“ã„ã¤ã‚’requireã™ã‚Œã°ã‚ˆã•ãã†ã§ã™ã。 ã“ã“ã®ã€1 How to Load Core ExtensionsãŒå‚考ã«ãªã‚Šã¾ã™ã€‚ http://guides.rubyonrails.org/active_support_core_extensions.html 例ãˆã°ã€active_s
ã“ã‚“ã«ã¡ã¯ã€éˆ´æœ¨ã§ã™ã€‚ 便利ãªãƒ¡ã‚½ãƒƒãƒ‰ã‚„イディオムã®å˜åœ¨ã‚’知ã£ã¦ã—ã¾ã£ãŸã¨ãã®æ¥½ã—ã•ã¯ã€Ruby/Railsを使ã†ç†ç”±ã®ä¸€ã¤ã§ã¯ãªã„ã§ã—ょã†ã‹ã€‚ 今回ã¯ã€ä¾¿åˆ©ãƒ¡ã‚½ãƒƒãƒ‰ã®å®åº« ActiveSupport ã‹ã‚‰ã„ãã¤ã‹ã”紹介ã—ãŸã„ã¨æ€ã„ã¾ã™ã€‚
最近ã€Rubyã§Railsを使ã‚ãšã«é–‹ç™ºã‚’ã™ã‚‹æ©Ÿä¼šãŒã‚ã‚‹ã®ã§ã™ãŒã€æ—¥ä»˜ã«é–¢ã™ã‚‹è¨ˆç®—ã¯çµæ§‹é¢å€’ãªã“ã¨ãŒå¤šãã€ã‚³ãƒ¼ãƒ‰ã‚‚汚ããªã£ã¦ã—ã¾ã„ãŒã¡ã§ã™ã€‚ ã—ã‹ã—ã€railsã§ãŠãªã˜ã¿ã®active_supportを使ãˆã°ã€ç›´æ„Ÿçš„ã«æ›¸ã‘ã¦ã—ã¾ã„ã¾ã™ã€‚ ã‚„ã¯ã‚Šactive_supportã¯å‰å¤§ã§ã™ã€‚ require 'rubygems' require 'active_support/all' date = Date.today #<Date: 2013-06-08 ((2456452j,0s,0n),+0s,2299161j)> date.tommorrow # 翌日(Sun, 09 Jun 2013) date.yesterday # 昨日(Fri, 07 Jun 2013) date.next_year # æ¥å¹´ï¼ˆSun, 08 Jun 2014) date.prev_year # 昨年(Fri,
ランã‚ング
ランã‚ング
ランã‚ング
リリースã€éšœå®³æƒ…å ±ãªã©ã®ã‚µãƒ¼ãƒ“スã®ãŠçŸ¥ã‚‰ã›
最新ã®äººæ°—エントリーã®é…ä¿¡
j次ã®ãƒ–ックマーク
kå‰ã®ãƒ–ックマーク
lã‚ã¨ã§èªã‚€
eコメント一覧を開ã
oページを開ã
{{#tags}}- {{label}}
{{/tags}}