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}}