22. WritewithRuby
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.makeKeyAndVisible
@blue_view = UIView.alloc.initWithFrame(CGRectMake(10, 10, 100, 100))
@blue_view.backgroundColor = UIColor.blueColor
@window.addSubview(@blue_view)
true
end
end
app_delegate.rb
23. UsePowerofRuby
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
@window.makeKeyAndVisible
%w(blue-lagoon red-ocean black-coffee).each_with_index do |str, i|
UIView.alloc.init.tap do |view|
view.frame = [[50 * i, 50 * i + 20], [100, 100]]
view.backgroundColor = UIColor.send(:"#{$1}Color")
@window.addSubview(view)
end if str =~ /^(?!black)(w+)-.+/
end
true
end
end
app_delegate.rb
24. Metaprogramming
http://clayallsopp.com/posts/rubymotion-metaprogramming/
def tableView(tableView,
didSelectRowAtIndexPath:indexPath)
menu_row = self.menu_items[indexPath.row]
# => 'profile'
self.send("open_#{menu_row}")
end
def open_profile; #...; end
def open_messages; #...; end
def open_feed; # ...; end
class Profile
# EX: has_one :user
def self.has_one(name)
klass = make_klass_from_name(name)
# EX: => User
# Effect: (a_profile.user = a_user)
# === a_user.profile_id = 4
define_method("#{name}=") do |value|
value.send("#{self.class.stringify}_id=", self.id)
value.save
value
end
end
def self.stringify; "profile"; end
end
send define_method
29. motion-cocoapods
Rakefile
# -‐*-‐ coding: utf-‐8 -‐*-‐
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
require 'rubygems'
require 'motion-‐cocoapods'
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'HelloMotion'
app.pods do
pod 'JSONKit'
end
end
30. Bundler
Rakefile
# -‐*-‐ coding: utf-‐8 -‐*-‐
$:.unshift("/Library/RubyMotion/lib")
require 'motion/project'
require 'bundler'
Bundler.require
Motion::Project::App.setup do |app|
# Use `rake config' to see complete project settings.
app.name = 'HelloMotion'
app.pods do
pod 'JSONKit'
end
end
Gemfile
source 'https://rubygems.org'
gem 'rake'
gem 'motion-‐cocoapods'
gem 'bubble-‐wrap'
gem 'sugarcube'
31. BubbleWrap
# Uses the front camera
BW::Device.camera.front.picture(media_types: [:movie, :image]) do |result|
image_view = UIImageView.alloc.initWithImage(result[:original_image])
end
Camera
BW::Location.get do |result|
p "From Lat #{result[:from].latitude}, Long #{result[:from].longitude}"
p "To Lat #{result[:to].latitude}, Long #{result[:to].longitude}"
end
Location
BW::HTTP.get("https://api.github.com/users/mattetti") do |response|
p response.body.to_str
end
HTTP