Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Por que Ruby?

Por que Ruby?

Dentre tantas linguagens de programação existentes no mercado, por que Ruby tem atraído tanta gente? Nesse talk irei falar o que chamou minha atenção nessa nessa linguagem que tanto amamos.

Samuel Flores

April 27, 2013
Tweet

More Decks by Samuel Flores

Other Decks in Programming

Transcript

  1. class Person { private String name; private Date birthday; public

    String getName() { return name; } public void setName(String value) { name = value; } public String getBirthday() { return birthday; } public void setBirtday(String value) { birthday = value; } }
  2. class Person { private String name; private Date birthday; public

    String getName() { return name; } public void setName(String value) { name = value; } public String getBirthday() { return birthday; } public void setBirtday(String value) { birthday = value; } } class Person attr_accessor :name, :birthday end
  3. public void deliver() { if (message.length != 0) { connection.send(message)

    } } def deliver connection.send(message) unless message.empty? end
  4. // int[] list = [1, 2, 3, ..., 100]; for

    (int i = 0; i < list.length; i++) { printf("%d", list[i]); }
  5. // int[] list = [1, 2, 3, ..., 100]; for

    (int i = 0; i < list.length; i++) { printf("%d", list[i]); } # list = (1..100).to_a list.each { |i| puts i }
  6. state_machine :status, initial: :draft do event :deliver do transition draft:

    :sent end before_transition on: :deliver do throw :halt if receivers.empty? notify end end
  7. Map map = new HashMap(); map.put("um", 1); map.put("dois", 2); map.put("três",

    3); map = { "um" => 1, "dois" => 2, "três" => 3 }
  8. String cpf = "123.456.789-00"; Pattern pattern = Pattern.compile("^\\d{3}\\.\\d{3}\\.\\d{3}-\\d{2}$"); Matcher matcher

    = pattern.matcher( cpf ); if (matcher.matches()) { System.out.println( "Formato de CPF válido" ); }
  9. String cpf = "123.456.789-00"; Pattern pattern = Pattern.compile("^\\d{3}\\.\\d{3}\\.\\d{3}-\\d{2}$"); Matcher matcher

    = pattern.matcher( cpf ); if (matcher.matches()) { System.out.println( "Formato de CPF válido" ); } cpf = "123.456.789-00" if cpf =~ /^\d{3}\.\d{3}\.\d{3}-\d{2}$/ puts "Formato de CPF válido" end
  10. File.open("filename.txt") do |file| file.write("Some text") end (1..200).select do |number| number.prime?

    end code = -> { print "Hello world" } code.call do |file| file.write("Some text") end do |number| number.prime? end -> { print "Hello world" }
  11. class Fixnum def prime? return false if self < 2

    return true if self == 2 factor = (2..(self / 2)).detect { |n| self % n == 0 } factor.nil? end end 31.prime? # => true
  12. class Person < ActiveRecord::Base # no code here end person

    = Person.find_by_name_and_surname("Samuel", "Flores") person.name # => John person.birtday # => 1984-04-29
  13. <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>${h2database.version}</version>

    <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> <scope>test</scope> </dependency>
  14. <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>${h2database.version}</version>

    <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework-version}</version> <scope>test</scope> </dependency> source "https://rubygems.org" gem 'sinatra', '1.4.1' gem 'dm-core', '1.2.0' gem 'dm-types', '1.2.2' gem 'dm-sqlite-adapter', '1.2.0' gem 'jdbc-sqlite3', '3.7.2', platforms: :jruby gem 'dm-zim-adapter', '0.0.1', platforms: :jruby gem 'do_zim', '0.0.1', platforms: :jruby gem 'dm-aggregates', '1.2.0' gem 'dm-validations', '1.2.0' gem 'dm-timestamps', '1.2.0' gem 'dm-serializer', '1.2.2' gem 'dm-migrations', '1.2.0'
  15. <macrodef name="do-only-if-not-library"> <attribute name="elseText" /> <element name="task-to-do" implicit="yes" /> <sequential>

    <if condition="${project.is.library}"> <else> <task-to-do /> </else> <then> <echo level="info">@{elseText}</echo> </then> </if> </sequential> </macrodef>
  16. <macrodef name="do-only-if-not-library"> <attribute name="elseText" /> <element name="task-to-do" implicit="yes" /> <sequential>

    <if condition="${project.is.library}"> <else> <task-to-do /> </else> <then> <echo level="info">@{elseText}</echo> </then> </if> </sequential> </macrodef> desc 'Seed the DB' task :seed do Rake::Task['db:reset'].invoke seeds = TOML.load_file("db/seed.toml") time = Time.now seeds['user'].each do |key, attributes| User.create!(attributes.merge(created_at: time)) end end
  17. class CalculatorTest < ActiveSupport::TestCase test "sum operation with more than

    2 operands" do calc = Calculator.new assert calc.sum(1, 2, 3) == 6 end end
  18. describe CalculatorTest do let(:calc) { Calculator.new } it "sums more

    than 2 operands" do calc.sum(1, 2, 3).should == 6 end end
  19. Scenario: add more than two numbers Given I have entered

    1 into the calculator And I have pressed the plus button And I have entered 2 into the calculator And I have pressed the plus button And I have entered 3 into the calculator When I press the equal button Then the result should be 6 on the screen
  20. Web

  21. > rails generate scaffold person name:string birthday:date active_record db/migrate/20130427142248_create_people.rb app/models/person.rb

    resource_route resources :people scaffold_controller app/controllers/people_controller.rb erb app/views/people app/views/people/index.html.erb app/views/people/edit.html.erb app/views/people/show.html.erb app/views/people/new.html.erb app/views/people/_form.html.erb . . .
  22. Shoes.app :height => 150, :width => 250 do background rgb(240,

    250, 208) stack :margin => 10 do button "Start" do @time = Time.now @label.replace "Stop watch started at #@time" end button "Stop" do @label.replace "Stopped, ", strong("#{Time.now - @time}"), " seconds elapsed." end @label = para "Press ", strong("start"), " to begin timing." end end
  23. Shoes.app :height => 150, :width => 250 do background rgb(240,

    250, 208) stack :margin => 10 do button "Start" do @time = Time.now @label.replace "Stop watch started at #@time" end button "Stop" do @label.replace "Stopped, ", strong("#{Time.now - @time}"), " seconds elapsed." end @label = para "Press ", strong("start"), " to begin timing." end end