I tried to add the sugar of Swift to RubyMotion. (It would be awesome if it was RubyMotion native. cc @lrz) Unfortunately, I am still not satisfied by the result.
Please don't use it in production!
gem 'motion-initializers', git: 'https://github.com/n-studio/motion-initializers.git'
view = UIView.alloc.initWithFrame([[100, 100], [100, 50]]) # before
view = UIView.new(frame: [[100, 100], [100, 50]]) # after
@button = UIButton.new(frame: [[100, view.maxY], [100, 50]])
@button.font = UIFont.new(name: "MarkerFelt-Thin", size: 45)
@button.set_title("Click me", state: :normal) # Ruby way
# or
@button.setTitle!("Click me", forState: :normal) # Cocoa way
@button.add_target(self, action: "pressed:", events: :touch_up_inside) # shorter
# or
@button.addTarget!(self, action: "pressed:", forControlEvents: :touch_up_inside) # easier to guess from Apple documentation
@button._?{maybe_not_defined_method.other_method}
@button._?{maybe_not_defined_method}._?{maybe_not_defined_method}
@button._?('maybe_not_defined_method:arg2:', arg1, arg2)._?('maybe_not_defined_method')
if (secondRoomName = john.residence._?{|r| r[1]}._?{name})
p "The second room name is #{secondRoomName}."
elsif (firstRoomName = john.residence._?{first}._?{name})
p "The first room name is #{firstRoomName}."
else
p "Unable to retrieve the first or second room name."
end
@button._!{maybe_not_defined_method}
=> raise an error if button is nil
That is said, I don't find this syntax very nice.
Swift
john.residence?[0].name
Ruby
john.residence && john.residence[0] ? john.residence[0].name : nil # old way
john.residence._?{|r| r[0]}._?{name} # nay
john.residence._?{fetch(0, nil)}._?{name} # nay
Ruby is still longer/more verbose than Swift!! Any idea?
- Suggest better syntax
or
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Check there is no memory leak (veryyyyy important)
- Push to the branch (
git push origin my-new-feature
) - Create new Pull Request