rod mclaughlin


Ruby Two's Day (27 jun 13)

http://www.ruby-lang.org/en/news/2013/02/24/ruby-2-0-0-p0-is-released/

 

Also, Rails Four:

http://edgeguides.rubyonrails.org/4_0_release_notes.html

 


 Ruby 2.0                

module Strings

  refine String do

    def bang
      "#{self}!"
    end

  end

end

begin

  s = "hello"
  puts s.bang

rescue Exception => x

  puts "s.bang caused an error: "+x.message
  puts "trying s.bang, using module with bang in it first"
  using Strings
  puts s.bang

end

class Foo

   SYMBOLS = %I{ foo bar }
 
   def bar(one: "First argument", two: "")
     puts two + " " + one
     puts SYMBOLS.map( &:to_s ).join( ' ' )
     puts __dir__
   end

end

foo = Foo.new

foo.bar

foo.bar( two: "Second argument" )

Bar = Struct.new( :bar, :foo ) do

# nothing

end

bar = Bar.new("FOO", "BAR").to_h
bar.each { |k,v| print "#{k}=>#{v} " }
puts

module Prepender
  def bar(one: "1st argument", two: "")

    puts one + " " + two # the correct order

  end
end

class Prepended

  prepend Prepender

  def bar(one: "argument 1", two: "argument 2")

    puts two + " " + one # the incorrect order

  end

end

pre = Prepended.new 
# the method is overridden by the one in Prepender, but arguments are as below

pre.bar( two: "2nd argument", one: "1st argument" ) 
# listed in incorrect order, printed in correct order

 
class Who

  def whose_there_using_locations

   caller_locations(1,1).size.times { |t| 
      puts "#{t + 1}. #{ caller_locations(1,1)[t].label }" 
   }

  end

end

class There

  def who
    who = Who.new
    who.whose_there_using_locations
  end

end

There.new.who

 
# http://blog.marc-andre.ca/2013/02/23/ruby-2-by-example
puts "Thanks Marc André from whom most of this was copied ❤" # utf-8



By the way, it's more difficult to install Ruby 2.0.0 on Mac than on Windows! You run 'brew update'
and it tells you to get Xcode, which you have to get from Apple...

 $ brew update

 [You have to get the latest Xcode from the Apple website]

$ rvm install ruby-2.0.0

 Install of ruby-2.0.0-p247 - #complete
  'rvm_codesign_identity' is not set so RVM can't sign ruby
   Set it in ~/.rvmrc after reading the following about OS X code signing:
   https://developer.apple.com/library/mac/
    #documentation/Security/Conceptual/CodeSigningGuide/Introduction/Introduction.html

$ rvm use 2.0.0-p247

 Using ~/.rvm/gems/ruby-2.0.0-p247

$ ruby -v

 ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin11.4.2]

$ gem install rails --version 4.0.0 --no-ri --no-rdoc

$ bundle update

$ rails server&
 [2013-06-30 13:33:04] INFO  WEBrick 1.3.1
 [2013-06-30 13:33:04] INFO  ruby 2.0.0 (2013-06-27) [x86_64-darwin11.4.2]
 [2013-06-30 13:33:04] INFO  WEBrick::HTTPServer#start: pid=47210 port=3000

Gemfile:

source 'https://rubygems.org'

gem 'rails', '4.0.0'

gem 'sqlite3'
gem 'jquery-rails'
gem 'sass'
gem 'coffee-rails'

group :assets do
  gem 'uglifier', '>= 1.0.3'
end

More about the complexity of using Apple computers for Rails here:

http://rodmclaughlin.com/why-do-rails-developers-use-macs-



Back
Portland London